Skip to content

Fix LeaderElector waiting a full extra lease duration before acquiring an already-expired lease on startup - #4914

Merged
kubernetes-prow[bot] merged 1 commit into
masterfrom
copilot/fix-leader-elector-expired-lease
Oct 16, 2026
Merged

kubernetes-prow[bot] merged 1 commit into
masterfrom
copilot/fix-leader-elector-expired-lease

Conversation

Copilot AI commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

On first observing an existing lock record, LeaderElector stamped its internal observedTimeMilliSeconds with the current wall-clock time rather than the record's actual renewTime. This made a freshly-started candidate treat an already-expired lease as still valid for one more full leaseDuration, delaying takeover instead of acquiring it immediately.

  • LeaderElector.tryAcquireOrRenew: when the observed record is being set for the very first time (no prior observedRecord), initialize observedTimeMilliSeconds from the record's own renewTime instead of System.currentTimeMillis(). Subsequent observations during normal operation still use the current time, preserving the existing clock-skew tolerance.
  • Test: added acquiresAlreadyExpiredLeaseImmediately in LeaderElectorTest, which seeds a lock with a record whose renewTime is well past leaseDuration and asserts a new candidate acquires leadership promptly rather than waiting out an extra lease period.
if (!oldLeaderElectionRecord.equals(this.observedRecord)) {
  boolean firstObservation = this.observedRecord == null;
  this.observedRecord = oldLeaderElectionRecord;
  this.observedTimeMilliSeconds =
      firstObservation
          ? oldLeaderElectionRecord.getRenewTime().getTime()
          : System.currentTimeMillis();
}

@kubernetes-prow kubernetes-prow Bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 18, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: brendandburns / name: brendandburns (ad828d3)

@kubernetes-prow kubernetes-prow Bot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Sep 18, 2026
@kubernetes-prow kubernetes-prow Bot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Sep 18, 2026
Copilot AI changed the title [WIP] Fix LeaderElector fails to acquire expired lease on initial run Fix LeaderElector waiting a full extra lease duration before acquiring an already-expired lease on startup Sep 18, 2026
Copilot AI requested a review from brendandburns September 18, 2026 14:32
@brendandburns
brendandburns marked this pull request as ready for review October 16, 2026 20:30
@kubernetes-prow kubernetes-prow Bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 16, 2026
@brendandburns

Copy link
Copy Markdown
Contributor

/lgtm
/approve

@kubernetes-prow
kubernetes-prow Bot requested a review from yue9944882 October 16, 2026 20:30
@kubernetes-prow kubernetes-prow Bot added lgtm "Looks good to me", indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Oct 16, 2026
@brendandburns
brendandburns force-pushed the copilot/fix-leader-elector-expired-lease branch from 6a86c93 to be14463 Compare October 16, 2026 20:44
@kubernetes-prow kubernetes-prow Bot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 16, 2026
@brendandburns
brendandburns force-pushed the copilot/fix-leader-elector-expired-lease branch from be14463 to ad828d3 Compare October 16, 2026 20:49
@kubernetes-prow kubernetes-prow Bot 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 Oct 16, 2026
@brendandburns

Copy link
Copy Markdown
Contributor

/lgtm
/approve

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 16, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brendandburns, Copilot

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

The pull request process is described 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 merged commit 7bb502a into master Oct 16, 2026
14 checks passed
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/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.

LeaderElector fails to acquire expired lease on initial run due to missing expiration check

2 participants