Skip to content

Stop leading when onstarted_leading raises - #2670

Open
LALITH0110 wants to merge 1 commit into
kubernetes-client:masterfrom
LALITH0110:leaderelection-stop-on-callback-exception
Open

Stop leading when onstarted_leading raises#2670
LALITH0110 wants to merge 1 commit into
kubernetes-client:masterfrom
LALITH0110:leaderelection-stop-on-callback-exception

Conversation

@LALITH0110

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

onstarted_leading is started on its own thread, so when it raises, the
exception is swallowed by that thread and the renew loop never learns about it.
The candidate keeps renewing its lease indefinitely even though the work the
lease is meant to protect has stopped, and onstopped_leading is never called.
Because the lease keeps being renewed, no other candidate can take over either.

This records the failure and checks it at the top of the renew loop, so the
candidate stops renewing and run() calls onstopped_leading — the same path
already taken when a renewal fails.

Scope: only the failure case reported in the issue changes. A callback that
returns normally behaves exactly as before, and none of the existing tests
needed to be modified.

Reproduced before fixing, with a lock that never refuses a renewal (so the only
thing that can end the leadership is the callback):

renewals after the callback died onstopped_leading called
before 8 (unbounded) no
after 1 yes

Which issue(s) this PR fixes:

Fixes #2075

Special notes for your reviewer:

  • The new test uses a MockResourceLock whose renew_count_max is
    sys.maxsize, so the renew loop can only exit because the callback failed —
    it fails on master (times out) and passes with this change. It runs the
    election on a daemon thread so a regression times out rather than hanging CI.
  • renew_loop keeps its old zero-argument form via a default of None, so any
    existing caller is unaffected.
  • Only exceptions are caught; BaseException is deliberately not caught, so
    KeyboardInterrupt and SystemExit keep their current behaviour.
  • Verified locally on macOS (Python 3.12): full suite minus e2e is 1484
    passed, 42 skipped, 0 failed
    ; the leaderelection suite goes 4 -> 5 passed.
    flake8 --select=E9,F63,F7,F82 is clean, and pycodestyle reports exactly
    the same pre-existing findings before and after this change.

Does this PR introduce a user-facing change?

Leader election now stops leading and calls `onstopped_leading` when the `onstarted_leading` callback raises, instead of renewing the lease indefinitely. The exception is logged via the `leaderelection` logger.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


The onstarted_leading callback runs in its own thread, so an exception
raised there was swallowed by that thread and never observed by the
renew loop. The candidate kept renewing its lease indefinitely while the
work the lease was meant to protect was no longer running, and
onstopped_leading was never called.

Record the failure and check it in the renew loop, so the candidate stops
renewing and run() invokes onstopped_leading as it already does when a
renewal fails.

A callback that returns normally is left alone; only the failure case
reported in the issue changes.
@kubernetes-prow kubernetes-prow Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/bug Categorizes issue or PR as related to a bug. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 5, 2026
@kubernetes-prow
kubernetes-prow Bot requested a review from fabianvf August 5, 2026 16:27
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: LALITH0110
Once this PR has been reviewed and has the lgtm label, please assign yliaog for approval. For more information see the Code Review Process.

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

Details 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

@kubernetes-prow
kubernetes-prow Bot requested a review from roycaihw August 5, 2026 16:27
@kubernetes-prow kubernetes-prow Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Aug 5, 2026
@yliaog

yliaog commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

please investigate the CI test failures

@yliaog

yliaog commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

/close

@kubernetes-prow kubernetes-prow Bot closed this Aug 6, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

@yliaog: Closed this PR.

Details

In response to this:

/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-sigs/prow repository.

@yliaog

yliaog commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

/reopen

@kubernetes-prow kubernetes-prow Bot reopened this Aug 6, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

@yliaog: Reopened this PR.

Details

In response to this:

/reopen

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-sigs/prow 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. kind/bug Categorizes issue or PR as related to a bug. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

leaderelection do not stop leading properly

2 participants