Repository navigation
fix(webhooks): lease heartbeat renewal and stale reclaim DLQ transiti… - #1757
Merged
Baskarayelu merged 1 commit intoOct 22, 2026
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix(webhooks): lease heartbeat renewal and stale reclaim DLQ transition
Closes #1737
Summary of Changes
This PR resolves two webhook processor lease and concurrency defects in
lib/webhooks/processor.ts:WEBHOOK_RETRY_CONFIG.leaseTimeoutMs(default 30s) could have their events reclaimed by another worker while the first execution was still in-flight, violating single-execution concurrency guarantees.retryCount, causing them to re-execute indefinitely and never reach the Dead-Letter Queue (DLQ).Failure Modes Explained
1. Handler Lease Expiry Re-run
When
processWebhookEventclaimed an event, it recorded a single timestamp (updatedAt = now) and an expiration deadline (nextRetryAt = now + leaseTimeoutMs). While the handler executed, nothing extended this lease. Ifawait handler(payload)ran longer than 30s, the event matched the stale-processing filter (updatedAt <= now - leaseTimeoutMs), allowing a second worker to acquire the claim and run the handler concurrently. Although the first worker's finalizer eventually returned conflict upon seeing an unexpectedupdatedAt, duplicate external side effects had already occurred.2. Stale Reclaim Loop Without DLQ Transition
When an event caused an unhandled crash or process termination,
handleWebhookProcessingFailurenever ran. Subsequent worker cycles reclaimed the event by matching{ status: 'processing', updatedAt: { lte: staleProcessingBefore } }and simply resettingupdatedAtwithout bumpingretryCount. BecauseretryCountnever increased across abandoned attempts, the conditionnextRetryCount > event.maxRetrieswas never satisfied, resulting in infinite recurring crashes.Implementation Details
Heartbeat Lease Renewal (
leaseTimeoutMs / 3):handler(payload)executes, a background interval triggers periodic lease renewals atMath.floor(leaseTimeoutMs / 3)(e.g., every 10s for default 30s lease; 33ms for 100ms test lease).{ id: eventId, status: 'processing', updatedAt: currentToken }.currentTokenupdates to the new timestamp.count === 0(lease lost to another worker or manual intervention), the worker marksisOwner = false, stops the heartbeat, discards the handler result, and returnsstatus: 'conflict'without modifying database state.currentToken.Atomic Stale Reclaim & DLQ Transition:
processingevent (updatedAt <= staleProcessingBefore):event.retryCount >= event.maxRetries, the event is transitioned directly todlqwithlastError = 'LEASE_EXPIRED', an audit event (webhook.dlq) is emitted viarecordAuditEvent, and the handler is skipped.event.retryCount < event.maxRetries, the claim atomically incrementsretryCount: { increment: 1 }in the database, counting the abandoned attempt toward the retry limit.Concurrency Documentation:
docs/WEBHOOK_CONCURRENCY.mdto document the heartbeat lease renewal mechanism and the reclaim-counts-as-attempt invariant.Key Claim and Renewal Predicates
1. Active vs Stale Lease Detection
2. Stale Reclaim DLQ Transition
3. Stale Reclaim Atomic Increment
4. Heartbeat Lease Renewal
Verification & Test Results
All unit tests pass cleanly from the repo root via
npx vitest run tests/unit/webhook-*.test.ts:TypeScript type checking (
npx tsc --noEmit) passes with zero errors.