Skip to content

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

Closed

Description

@pdettori

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-worker pod does XREADGROUP > ... it claims entries into the group's Pending Entries List (PEL), then is expected to XACK after 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 runs XAUTOCLAIM/XCLAIM to reclaim idle PEL entries.
  • So pendingEntriesCount never drops below the threshold → KEDA keeps the ScaledJob ACTIVE → it keeps launching pods that find no new work (lag=0), exit Complete/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

  1. Enqueue one or more leaves (POST /run-leaf {async:true} → leaf-queue).
  2. Cause a worker to exit after XREADGROUP but before XACK — e.g. kill/evict the pod mid-run, or have the leaf hit a terminal error on a path that doesn't ACK.
  3. XINFO GROUPS leaf-queue → pending>0, lag=0; kubectl get scaledjob leaf-worker stays ACTIVE=True and keeps spawning pods even though no new work exists.

Suggested fixes

  1. Reaper via XAUTOCLAIM: have leaf-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 by sessionId) or dead-letter + XACK. This is the standard fix for the redis-streams PEL-orphan problem and lets pendingEntriesCount actually fall.
  2. ACK on every terminal outcome: ensure leaf-job XACKs after writing the done-marker, including on failed/aborted paths (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).
  3. Consumer GC: XGROUP DELCONSUMER for the pod's consumer in a finally/exit hook, or a periodic sweep of idle consumers, to bound the consumer list.
  4. Optionally reconsider the pendingEntriesCount trigger 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-worker

Within 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions