Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Fix a race condition between leader election and recorder #1379

Merged
merged 2 commits into from Feb 10, 2021

Conversation

vincepri
Copy link
Member

@vincepri vincepri commented Feb 9, 2021

This change introduces better syncronization between the leader election
code and the event recorder. Running tests with -race flag, we often saw
a panic on a closed channel, the channel was the one that the event
recorder was using internally.

After digging more through the code, it seems that we weren't properly
waiting for leader election code to stop completely, but instead we were
only calling the cancel() function asking the leader election to stop.

With this change, during a shutdown, we now wait for leader election to
finish up any internal task before we return and close an internal
channel. Only after leader election signals that the channel has been
closed, and Run(...) has properly returned, we return execution to the
stop procedure, where the event recorder is then stopped.

Signed-off-by: Vince Prignano vincepri@vmware.com

This change introduces better syncronization between the leader election
code and the event recorder. Running tests with -race flag, we often saw
a panic on a closed channel, the channel was the one that the event
recorder was using internally.

After digging more through the code, it seems that we weren't properly
waiting for leader election code to stop completely, but instead we were
only calling the cancel() function asking the leader election to stop.

With this change, during a shutdown, we now wait for leader election to
finish up any internal task before we return and close an internal
channel. Only after leader election signals that the channel has been
closed, and  Run(...) has properly returned, we return execution to the
stop procedure, where the event recorder is then stopped.

Signed-off-by: Vince Prignano <vincepri@vmware.com>
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Feb 9, 2021
@vincepri
Copy link
Member Author

vincepri commented Feb 9, 2021

/milestone v0.9.0

@k8s-ci-robot
Copy link
Contributor

@vincepri: The provided milestone is not valid for this repository. Milestones in this repository: [1.0.0, Next, v0.5.x, v0.6.x, v0.7.x, v0.8.x, v0.9.x]

Use /milestone clear to clear the milestone.

In response to this:

/milestone v0.9.0

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@vincepri
Copy link
Member Author

vincepri commented Feb 9, 2021

/milestone v0.9.x

@k8s-ci-robot k8s-ci-robot added this to the v0.9.x milestone Feb 9, 2021
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 9, 2021
@vincepri
Copy link
Member Author

vincepri commented Feb 9, 2021

@christopherhein
Copy link
Member

Nice, this looks good to me, I will wait for others to chime in.

cm.leaderElectionCancel()
<-cm.leaderElectionStopped
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One concern (Probably more about the unconditional leaderElectionCancel() call than your change):
If we attempt to shut down gracefully and the Runnables do not end in time, they will end up running after we are not elected leader anymore (since we now block until that happens).

Maybe cancel the leader election only if the rest of the func has a nil error or an error that is not context.Cancelled?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean checking the return err and only if it's not nil, cancel leader election? The error comes from shutdownCtx, if we don't cancel leader election, I'd assume that it'll timeout after a bit and the main.go should exit, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to double check the thinking, I'd also like to capture in a comment around the err check

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only if its nil, cancel leader election, if its not-nil, that means something is still running and its unsafe to cancel leader election because that might result in the thing still running after we are not leader anymore.

This should result in the binary exiting which exists all pending routines and the leader lease of us will just time out.

Signed-off-by: Vince Prignano <vincepri@vmware.com>
@vincepri
Copy link
Member Author

@alvaroaleman ptal

Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 10, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alvaroaleman, vincepri

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [alvaroaleman,vincepri]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants