Skip to content

Fix: PEL reaper + consumer GC to prevent leaf-worker KEDA deadlock - #22

Merged
pdettori merged 2 commits into
mainfrom
fix/pel-reaper-17
Jun 29, 2026
Merged

pdettori merged 2 commits into
mainfrom
fix/pel-reaper-17

Conversation

@pdettori

Copy link
Copy Markdown
Member

Summary

  • Adds deleteConsumer(), gcIdleConsumers(), reapDeadLetters() to @sh/work-queue
  • leaf-job startup sweeps stale PEL entries past maxAttempts → writes failed done-markers and ACKs them so pendingEntriesCount drops and KEDA can scale to zero
  • On clean exit: deleteConsumer() removes our consumer registration (bounds consumer list growth)
  • gcIdleConsumers() removes orphaned consumers with 0 pending entries idle > 5 min

Root cause

When a leaf-worker pod dies after XREADGROUP but before XACK, its entry stays in the PEL forever. KEDA's pendingEntriesCount trigger keeps the ScaledJob ACTIVE, spawning pods that find no reclaimable work (entries must be idle > 90s) and exit immediately — perpetual churn with no path to scale-to-zero.

How it works now

  1. Startup reap (reapDeadLetters): inspects PEL via XPENDING RANGE, selectively XCLAIM+XACK entries that are both stale (idle > 90s) AND past retry limit (deliveryCount > 3). Writes a failed done-marker so the orchestrator knows.
  2. Consumer GC (gcIdleConsumers): removes consumers with 0 pending entries idle > 5 min (orphaned by completed/crashed pods).
  3. Exit cleanup (deleteConsumer): on the idle/success exit path, removes our consumer so it doesn't accumulate.

Test plan

  • 5 new unit tests for deleteConsumer, gcIdleConsumers, reapDeadLetters (all hit real Redis)
  • Existing 4 work-queue tests still pass (9/9 total)
  • Existing leaf-job-runner tests still pass
  • TypeScript strict typecheck passes
  • Kind smoke test (deploy and verify scale-to-zero after stale PEL scenario)

Closes #17

Assisted-By: Claude Code

…KEDA deadlock

- Add deleteConsumer(), gcIdleConsumers(), reapDeadLetters() to WorkQueue
- leaf-job startup sweeps stale PEL entries past maxAttempts (dead-letters
  them with done-marker) so pendingEntriesCount drops and KEDA scales to zero
- On clean exit, deleteConsumer() removes our consumer registration
- gcIdleConsumers() removes orphaned consumers with 0 pending entries
- 5 new unit tests covering the new methods

Closes #17

Signed-off-by: Paolo Dettori <[email protected]>

@pdettori pdettori left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

(Posting as a comment review — GitHub won't let an author approve their own PR. No blocking issues; this would otherwise be an APPROVE.)

Clean, focused fix for the leaf-worker KEDA deadlock. The PEL reaper + consumer GC approach is sound, the comments explain the scale-to-zero rationale well, and the new queue methods are correct. Tests hit real Redis and cover both branches of each method.

Note on CI: only the DCO check runs in this repo (passing) — the 9/9 unit-test and TypeScript typecheck claims are local-only, not enforced by a CI workflow.

One suggestion and one nit inline (neither blocking).

Assisted-By: Claude Code

Comment thread packages/work-queue/src/queue.ts Resolved
Comment thread packages/work-queue/src/queue.ts Resolved
…ch const

- Skip ACK when xClaim returns empty (entry reclaimed by another consumer
  between xPendingRange inspect and xClaim)
- Wrap JSON.parse in try/catch so a malformed envelope doesn't crash the
  startup sweep; ACK still runs to clear the stale entry
- Extract REAP_BATCH=100 named const with comment explaining cross-startup
  drain behavior
- Log warning for entries dead-lettered with unrecoverable envelope

Addresses PR #22 review feedback.

Signed-off-by: Paolo Dettori <[email protected]>
@pdettori
pdettori merged commit 2c11f6d into main Jun 29, 2026
1 check passed
@pdettori
pdettori deleted the fix/pel-reaper-17 branch June 29, 2026 19:43
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.

leaf-worker KEDA ScaledJob never scales to zero: stale pending entries deadlock (no XAUTOCLAIM reaper)

1 participant