Skip to content

Fix stuck leases when worker restarts with pending checkpoint leases - #1718

Merged
chenylee-aws merged 1 commit into
awslabs:masterfrom
chenylee-aws:leaseshutdown
Mar 26, 2026
Merged

chenylee-aws merged 1 commit into
awslabs:masterfrom
chenylee-aws:leaseshutdown

Conversation

@chenylee-aws

Copy link
Copy Markdown
Contributor

Description of changes:

During graceful lease handoff from Worker A to Worker B, a lease can become stuck in a pending checkpoint state when Worker A fails to complete the checkpoint (e.g., due to spurious retries). This leaves the lease with checkpointOwner set to Worker A and leaseOwner set to Worker B.(A valid state)

If Worker B is running, it will not try to acquire the lease because it sees the lease is in pending checkpoint state and leaseDiscover would prevent it to acquire the lease. However, if Worker B restarts and initializes its
LeaseCoordinator, it discovers this lease during the initialize() call. The worker incorrectly acquires and immediately attempts to shut down the lease. The shutdown logic fails to release the lease because the worker never explicitly drops it, causing the heartbeat to continue indefinitely and leaving the lease stuck.

The change adds logic in the initialize method to also not acquire pendingCheckpoint leases as done in leaseDiscoverer. It also adds logic to not renew leases that's not in an expected state

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Comment on lines +215 to +221
if (!isCheckpointOwner(lease)) {
log.warn(
"Worker {} not renewing shutdown-requested lease {} with checkpointOwner {}",
workerIdentifier,
lease,
lease.checkpointOwner());
ownedLeases.remove(leaseKey);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this part necessary or extra safety precaution? I don't see how the pendingCheckpoint lease can be added to the ownedLeases anymore after the checkpointOwner check in initialize() line 499 and the existing check for leaseDiscoverer https://github.com/awslabs/amazon-kinesis-client/blob/master/amazon-kinesis-client/src/main/java/software/amazon/kinesis/leases/dynamodb/DynamoDBLeaseDiscoverer.java#L121

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is for extra safety precaution to prevent any future method to accidentally renew leases that they shouldn't renew


private void assignLease(final Lease lease, final WorkerMetricStats workerMetrics) {
if (nonNull(lease.actualOwner()) && lease.actualOwner().equals(workerMetrics.getWorkerId())) {
private void assignLease(final Lease lease, final WorkerMetricStats workerMetrics, boolean isExpiredLease) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: final

return renewLease(lease, false);
}

private boolean isCheckpointOwner(Lease lease) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: final

final WorkerMetricStats workerToAssignLease = assignableWorkerSortedByAvailableCapacity.poll();
if (nonNull(workerToAssignLease)) {
assignLease(lease, workerToAssignLease);
assignLease(lease, workerToAssignLease, true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Technically can be unassigned but not expired right? Is it possible to be unassigned but still have pendingCheckpoint?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Correct. In KCL, we don't expect leaseOwner attribute to be removed after lease is added to the system and assigned once. So unassigned leases should never have pending checkpoint unless they get manually edited for example

@chenylee-aws
chenylee-aws merged commit b4052e7 into awslabs:master Mar 26, 2026
7 of 8 checks passed
@chenylee-aws
chenylee-aws deleted the leaseshutdown branch March 26, 2026 03:39

This branch was previously deployed

1 inactive deployment
manual-approval β€” 3c8b7b04 Deployed Mar 19, 2026 by chenylee-aws via wait-for-approval #100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants