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ā€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

šŸ› fix bug when set renewDeadline greater than leaseDuration the controlā€¦ #1761

Closed
wants to merge 1 commit into from

Conversation

yuzp1996
Copy link

@yuzp1996 yuzp1996 commented Dec 30, 2021

fix bug when set renewDeadline greater than leaseDuration the controller will hang and no error will be return

Issue: #1760

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Dec 30, 2021

CLA Signed

The committers are authorized under a signed CLA.

  • āœ… äŗŽåæ—鹏 (e009b83)

@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

šŸ“ Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


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. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 30, 2021
@k8s-ci-robot
Copy link
Contributor

Welcome @yuzp1996!

It looks like this is your first PR to kubernetes-sigs/controller-runtime šŸŽ‰. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-runtime has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. šŸ˜ƒ

@k8s-ci-robot
Copy link
Contributor

Hi @yuzp1996. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@k8s-ci-robot k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Dec 30, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: yuzp1996
To complete the pull request process, please assign droot after the PR has been reviewed.
You can assign the PR to them by writing /assign @droot in a comment when ready.

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

Needs approval from an approver in each of these files:

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

@yuzp1996 yuzp1996 changed the title fix bug when set renewDeadline greater than leaseDuration the controlā€¦ šŸ› fix bug when set renewDeadline greater than leaseDuration the controlā€¦ Dec 30, 2021
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Dec 31, 2021
@@ -634,6 +634,7 @@ func (cm *controllerManager) startLeaderElection(ctx context.Context) (err error
ReleaseOnCancel: cm.leaderElectionReleaseOnCancel,
})
if err != nil {
Copy link
Author

Choose a reason for hiding this comment

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

if err happend the leaderElectionStopped will not close and engageStopProcedure will hang in this code

     defer func() {
        // Cancel leader election only after we waited. It will os.Exit() the app for safety.
        if cm.resourceLock != nil {
	        // After asking the context to be cancelled, make sure
	        // we wait for the leader stopped channel to be closed, otherwise
	        // we might encounter race conditions between this code
	        // and the event recorder, which is used within leader election code.
	        cm.leaderElectionCancel()
	        <-cm.leaderElectionStopped
        }
  }()

so I think when error happened we should close this channel

@FillZpp
Copy link
Contributor

FillZpp commented Dec 31, 2021

@yuzp1996 Yeah, it is a bug here, but not only for the NewLeaderElector error.

The cause of the manager hang during stopping, is the cm.leaderElectionStopped might not be closed. Sometimes the cm.leaderElectionCancel might be nil so it could also panic here. For example it fails to cm.add(cm.cluster) or cm.runnables.Webhooks.Start(cm.internalCtx) in the Start().

        // Cancel leader election only after we waited. It will os.Exit() the app for safety.
        if cm.resourceLock != nil {
	        // After asking the context to be cancelled, make sure
	        // we wait for the leader stopped channel to be closed, otherwise
	        // we might encounter race conditions between this code
	        // and the event recorder, which is used within leader election code.
	        cm.leaderElectionCancel()
	        <-cm.leaderElectionStopped
        }

So it might need to change like this, IMHO

  1. Init leaderElectionStopped only when NewLeaderElector succeeds.
  2. Should check if cm.leaderElectionCancel is nil before cm.leaderElectionCancel() and check if cm.leaderElectionStopped is nil before <-cm.leaderElectionStopped. If so, maybe there is no need to check if cm.resourceLock != nil here any more.

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Jan 3, 2022
@@ -637,6 +638,10 @@ func (cm *controllerManager) startLeaderElection(ctx context.Context) (err error
return err
}

ctx, cancel := context.WithCancel(context.Background())
Copy link
Author

Choose a reason for hiding this comment

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

Following @FillZpp's suggestion, I changed the location of initialization cm.leaderElectionCancel and cm.leaderElectionStopped. So these values ā€‹ā€‹will not be nil if NewLeaderElector succeeds.

When engageStopProcedure needs to check cm.leaderElectionCancel and cm.leaderElectionStopped, it will first check whether they are nil.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if this is correct, if we set the context and cancel here it'll be set in a goroutine which can cause a race condition if the controller closes too quickly.

Copy link
Contributor

@FillZpp FillZpp left a comment

Choose a reason for hiding this comment

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

Mostly LG. Some nits.
/ok-to-test

// we wait for the leader stopped channel to be closed, otherwise
// we might encounter race conditions between this code
// and the event recorder, which is used within leader election code.
if cm.leaderElectionCancel != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems you only have to check one of cm.leaderElectionCancel and cm.leaderElectionStopped for now they are assigned at the same place.

Copy link
Author

Choose a reason for hiding this comment

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

Yes but I think maybe checking them individually is safer šŸ˜„

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jan 4, 2022
@FillZpp
Copy link
Contributor

FillZpp commented Jan 4, 2022

@yuzp1996 And you have to fix the failed job, which you can test locally with make lint.

@yuzp1996 yuzp1996 closed this Jan 4, 2022
@yuzp1996 yuzp1996 reopened this Jan 4, 2022
ā€¦ler will hang and no error will be return

but actually the error should be throw and exit

Signed-off-by: yuzhipeng <yuzp1996@qq.com>
@FillZpp
Copy link
Contributor

FillZpp commented Jan 4, 2022

Thanks for fixing this.
/lgtm
/cc @vincepri @alvaroaleman PTAL

@k8s-ci-robot
Copy link
Contributor

@FillZpp: GitHub didn't allow me to request PR reviews from the following users: PTAL.

Note that only kubernetes-sigs members and repo collaborators can review this PR, and authors cannot review their own PRs.

In response to this:

Thanks for fixing this.
/lgtm
/cc @vincepri @alvaroaleman PTAL

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.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 4, 2022
Copy link
Member

@vincepri vincepri left a comment

Choose a reason for hiding this comment

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

Why do you want renewDeadline greater than lease duration? The lease should always be greater than its renewal, otherwise you might end up losing leader election which is probably what's happening here.

We could validate that the rule above applies and enforce it when configuring the manager, instead of changing the code around to avoid leader election.

/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jan 19, 2022
@yuzp1996
Copy link
Author

yuzp1996 commented Jan 19, 2022

Yes, I think we have had the validation but when someone set renewDeadline greater than lease duration the whole process will block and does not throw an error.

I don't think this is the correct way to do that so I make some changes to make sure errors can be thrown

@FillZpp
Copy link
Contributor

FillZpp commented Jan 20, 2022

@vincepri As I said above, it is not only caused by renewDeadline greater than lease duration.

Any error occurred between defer engageStopProcedure in controllerManager.Start() and // Start the leader elector process in controllerManager.startLeaderElection(), such as:

  1. Error of cm.add(cm.cluster)
  2. Error of cm.runnables.Webhooks.Start(cm.internalCtx)
  3. Error of cm.runnables.Caches.Start(cm.internalCtx)
  4. Error of cm.runnables.Others.Start(cm.internalCtx)
  5. Error of leaderelection.NewLeaderElector

They will all return error, trigger engageStopProcedure in defer, and finally hang on <-cm.leaderElectionStopped, because the channel will not be closed forever.

@FillZpp
Copy link
Contributor

FillZpp commented Jan 25, 2022

WDYT @vincepri

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 25, 2022
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels May 25, 2022
@FillZpp
Copy link
Contributor

FillZpp commented May 25, 2022

/remove-lifecycle rotten

@k8s-ci-robot k8s-ci-robot removed the lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. label May 25, 2022
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Aug 23, 2022
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Sep 22, 2022
@vincepri
Copy link
Member

Can we add a failing test that covers the above bug first which can help explain and prevent the deadlock above? I cannot reproduce locally, so it'd be good to have a test for this

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Reopen this PR with /reopen
  • Mark this PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

@k8s-ci-robot
Copy link
Contributor

@k8s-triage-robot: Closed this PR.

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Reopen this PR with /reopen
  • Mark this PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm "Looks good to me", indicates that a PR is ready to be merged. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. 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