Skip to content

[Bug]: Primary lease take-over polling can stall past RetryPeriod #11349

Description

@leonardoce

Is there an existing issue already for this bug?

  • I have searched for an existing issue, and could not find anything. I believe this is a new bug.

I have read the troubleshooting guide

  • I have read the troubleshooting guide and I think this is a new bug.

I am running a supported version of CloudNativePG

  • I have read the troubleshooting guide and I think this is a new bug.

Contact Details

[email protected]

Version

1.30.0

What version of Kubernetes are you using?

1.36

What is your Kubernetes environment?

Self-managed: kind (evaluation)

How did you install the operator?

YAML manifest

What happened?

tryTakeOver reads the primary lease via r.lock.Get(ctx), using the caller's context as-is with no per-call deadline. preAcquire calls tryTakeOver in a loop and expects each attempt to return within roughly RetryPeriod (2s by default), whether it succeeds, finds the lease unavailable, or errors out.

Because the Get call carried no timeout of its own, if the Kubernetes API server was unreachable or unresponsive, the call could block for however long the underlying client-go transport takes to give up on its own — not necessarily bounded by RetryPeriod. During that time the take-over loop made no progress: it couldn't observe the lease, couldn't detect a stale holder, and couldn't retry on schedule.

The practical effect is that a replica trying to take over a stale primary's lease (e.g. during a failover) could see its take-over attempts stall for an unpredictable, potentially long time whenever the API server was slow or unreachable, instead of retrying at the intended cadence.

Cluster resource

Relevant log output

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. self-assigned this
    on Aug 20, 2026
  2. added
    bug 🐛
    triage
    on Aug 20, 2026
  3. dosubot commented on Aug 20, 2026

    @dosubot

    @leonardoce, your analysis is spot on. In tryTakeOver, the r.lock.Get(ctx) call passes the caller's context with no per-call deadline [1]. The preAcquire loop only resets its timer to RetryPeriod after tryTakeOver returns [2], so a slow or unreachable API server can stall the entire polling cadence for however long the underlying transport takes to time out.

    The fix is straightforward: wrap r.lock.Get (and the r.lock.Update in claim) with a context.WithTimeout(ctx, r.config.RetryPeriod) so each API call is independently bounded:

    getCtx, cancel := context.WithTimeout(ctx, r.config.RetryPeriod)
    record, _, err := r.lock.Get(getCtx)
    cancel()

    This mirrors the pattern already used in runLeaderElection after le.Run exits, where a context.WithTimeout(ctx, r.config.RetryPeriod) is applied to the post-exit lease check [3].

    For context, the primary lease feature was introduced in #10627 and the defaultRetryPeriod was lowered from 5s to 2s in #10950, so this code is relatively new and the gap is understandable.

    To reply, just mention @dosu.


    Share context across your team and agents. Try Dosu.

    Leave Feedback Learn about cloudnative-pg with Dosu Add Dosu to your team

  4. added a commit that references this issue on Sep 8, 2026
    72075f0
  5. added a commit that references this issue on Sep 8, 2026
    a588239
  6. added this to the 1.30.1 milestone on Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug 🐛triage

Type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions