Repository navigation
Description
Summary
The leaf-worker KEDA ScaledJob (deploy/knative/leaf-scaledjob.yaml) can get stuck ACTIVE=True indefinitely, perpetually spawning worker pods, long after the leaf-queue is drained. Root cause is stale pending (delivered-but-unacked) entries in the leaf-workers consumer group that no live consumer will ever ACK, combined with the absence of any XAUTOCLAIM/reaper. A secondary leak is unbounded growth of the consumer registration list.
This is independent of any particular workload — it surfaced while running an external Archetype-A async fan-out against the harness, but the trigger is generic: any worker that reads an entry and then dies/exits before XACK (crash, OOM, eviction, non-zero exit, or an error mid-processing) leaks a pending entry.
Observed state (Kind, after ~41h)
XINFO GROUPS leaf-queue -> leaf-workers: consumers=11716 pending=4 lag=0
kubectl get scaledjob leaf-worker -> ACTIVE=True (age 41h)
kubectl get pods | grep leaf-worker -> 10 pods churning: Completed / Error / Running (age 2s)
lag=0 (no new, never-delivered entries) but pendingEntriesCount=4.
Root cause
leaf-scaledjob.yaml triggers on two redis-streams metrics: lagCount and pendingEntriesCount (both threshold 1).
- When a
leaf-workerpod doesXREADGROUP > ...it claims entries into the group's Pending Entries List (PEL), then is expected toXACKafter writing the result/done-marker. - If that pod dies/exits before
XACK(crash, eviction, non-zero exit, or any error on a failure path), the entry stays in the PEL forever. - New workers do
XREADGROUP >which only returns never-delivered entries — they do not see PEL entries owned by dead consumers. Nothing runsXAUTOCLAIM/XCLAIMto reclaim idle PEL entries. - So
pendingEntriesCountnever drops below the threshold → KEDA keeps the ScaledJobACTIVE→ it keeps launching pods that find no new work (lag=0), exitComplete/Error, and get relaunched. A perpetual loop.
Secondary: each leaf-worker pod registers a unique consumer name and never XGROUP DELCONSUMERs it on exit, so the consumer list grows without bound (11,716 observed).
Impact
- Perpetual pod churn (wasted compute) and ScaledJob never returns to scale-to-zero.
- If a churning worker ever claims+reprocesses an entry, that's wasted model/LLM calls.
- Obscures genuine scale-out (the queue looks permanently "active"), and accumulates orphaned consumers.
Reproduction
- Enqueue one or more leaves (
POST /run-leaf {async:true}→leaf-queue). - Cause a worker to exit after
XREADGROUPbut beforeXACK— e.g. kill/evict the pod mid-run, or have the leaf hit a terminal error on a path that doesn't ACK. XINFO GROUPS leaf-queue→pending>0,lag=0;kubectl get scaledjob leaf-workerstaysACTIVE=Trueand keeps spawning pods even though no new work exists.
Suggested fixes
- Reaper via
XAUTOCLAIM: haveleaf-job(on startup) or a small periodic CronJob claim PEL entries idle > N seconds (XAUTOCLAIM leaf-queue leaf-workers <consumer> <min-idle> 0), then either reprocess (idempotent bysessionId) or dead-letter +XACK. This is the standard fix for the redis-streams PEL-orphan problem and letspendingEntriesCountactually fall. - ACK on every terminal outcome: ensure
leaf-jobXACKs after writing the done-marker, including onfailed/abortedpaths (only an unclean crash should leak — and harness: tsc fails on cli.ts gateway-header cast (x-api-key: null→ Record<string, string>) #1 covers that). - Consumer GC:
XGROUP DELCONSUMERfor the pod's consumer in afinally/exit hook, or a periodic sweep of idle consumers, to bound the consumer list. - Optionally reconsider the
pendingEntriesCounttrigger threshold/role given it cannot self-clear without harness: tsc fails on cli.ts gateway-header cast (x-api-key: null→ Record<string, string>) #1.
Workaround (operational reset)
RPOD=$(kubectl -n default get pod -l app=redis -o jsonpath='{.items[0].metadata.name}')
kubectl -n default exec "$RPOD" -- redis-cli XGROUP DESTROY leaf-queue leaf-workers
kubectl -n default exec "$RPOD" -- redis-cli XGROUP CREATE leaf-queue leaf-workers '$' MKSTREAM
kubectl -n default exec "$RPOD" -- redis-cli XTRIM leaf-queue MAXLEN 0
kubectl -n default delete jobs -l scaledjob.keda.sh/name=leaf-workerWithin a poll cycle (~5s) the ScaledJob goes ACTIVE=False and leaf-worker pods drain to zero. The harness recreates the group on the next enqueue (XGROUP CREATE … MKSTREAM, BUSYGROUP-ignore), so the reset is safe.
Assisted-By: Claude Code
Activity