Skip to content

Stranded-jobs sweep keys on the queue label, so work stranded by actor assignment is invisible #211

Closed

Description

@rcbevans

Found while red-teaming operator visibility against real Postgres. Not previously filed.

Behaviour

Every fleet-wide "can this work ever run?" surface keys on the job row's queue label. But a re-pended row is routed by its actor's stored assignment (actor_config.queue), not by its label — the documented routing contract in backend/_dispatch_sql.py.

So a re-pended row whose actor's stored assignment names a queue no worker consumes is permanently unclaimable, while every surface reports health.

Measured

Five consecutive real dispatch rounds by the live consumer of the row's label queue claimed nothing. A control consumer of the assignment queue claimed it on the first round — proving the row is dispatchable work stranded by fleet subscriptions, not a malformed row.

The row stays status='pending', due, attempt=1, carrying a label naming a queue the fleet visibly serves.

Surface 1 — the stranded-jobs leader sweep (_leader_sweeps.py, _stranded_jobs_loop). Its unserved-queue predicate is:

NOT EXISTS (SELECT 1 FROM workers w WHERE j.queue = ANY(w.queues))

That is the label. Run for real against the seeded shape, the published gauge was {} — empty.

This is TaskQ's only surface that can decide "no worker anywhere serves this". A single booting worker cannot, which is exactly why spec Decision 9 makes boot-time detection a warning rather than a refusal. So when this surface is blind, nothing else covers the condition.

Operator impact

Work is unreachable by the entire fleet, and the one monitoring surface built to detect precisely that reports nothing wrong. The operator sees a healthy queue, a pending job, and a fleet that serves that job's queue label — with no way to discover that the routing decision uses a different value.

This is the failure mode the never-refuse-boot decision explicitly relies on telemetry to cover. The telemetry does not cover it.

Relationship to #187

#187 concerns jobs stranded by a queue move. This is the general case: any divergence between a row's label and its actor's stored assignment produces unclaimable work that the stranded-jobs sweep cannot see, regardless of how the divergence arose.

Fix direction

The stranded-jobs predicate must evaluate the same routing the dispatcher evaluates — the actor's stored assignment for claimed/re-pended rows, the label for never-claimed rows — so that "no worker serves this" is decided against the value that actually routes the work. Pin it by seeding the divergent shape and asserting the gauge names the stranded row.

Activity

  1. self-assigned this
    on Sep 15, 2026
  2. added
    bug
    on Sep 15, 2026
  3. rcbevans commented on Sep 15, 2026

    @rcbevans
    ContributorAuthor

    Verified against current code. Confirmed reproduces.

    Root cause, read directly:

    • src/taskq/worker/_leader_sweeps.py:1503-1508 — the stranded-jobs detector's unserved-queue predicate is NOT EXISTS (SELECT 1 FROM "{schema}".workers w WHERE j.queue = ANY(w.queues)). It always tests jobs.queue, the row's own label, regardless of the row's claim history.
    • src/taskq/backend/_dispatch_sql.py:76-90 ("Routing contract" docstring) — dispatch itself does not always route by label: a never-claimed row (started_at IS NULL) is routed by its own jobs.queue label, but a row that has been claimed at least once (started_at IS NOT NULL — every re-pend path: mark_retry/mark_failed_or_retry, the crash-reclaim sweep, operator retry_job, snooze/refund) is routed by its actor's current stored assignment (actor_config.queue), not the label. The label is left as an audit trail of first placement and no re-pend path rewrites it.

    So the detector's predicate and the dispatcher's actual routing diverge for exactly the re-pended population, which is a population the fix for #187 (move-queue tail-stranding) deliberately created re-pend/label divergence for. Any re-pended row whose actor's current assignment names a queue no worker consumes is permanently unclaimable, while the detector — the fleet's only surface able to decide "no worker anywhere serves this" (spec Decision 9: a single booting worker cannot know this, so refusal is never used and telemetry is relied on instead) — reports the condition as healthy because the label happens to name a served queue.

    Reproduction and observable symptom:

    tests/test_rt_orphans_stranded_detector_blind_spots.py::test_repended_row_routed_to_an_unserved_queue_is_undispatchable (control, currently GREEN) seeds a row for actor assignment_routed_actor whose stored assignment is unserved-assignment-queue but whose label reads served-label-queue, drives it through a real re-pend so started_at IS NOT NULL, then runs the real dispatch SQL:

    • A consumer of the row's label queue (served-label-queue) claims nothing across 5 real dispatch rounds — the row stays pending, due, carrying a label naming a queue the fleet visibly serves.
    • A control consumer of the actor's assignment queue (unserved-assignment-queue) claims it on the first round — proving the row is dispatchable work, stranded purely by fleet subscriptions relative to the assignment, not a malformed row.

    tests/test_rt_orphans_stranded_detector_blind_spots.py::test_stranded_detector_sees_the_assignment_routed_strand pins the detector itself against that exact seeded shape, running the real _stranded_jobs_loop. Currently RED:

    AssertionError: the stranded-jobs gauge must count the re-pended row routed to
    'unserved-assignment-queue', which no worker serves; the gauge published {}.
    assert 0 >= 1
    

    Command run:

    uv run --no-sync pytest tests/test_rt_orphans_stranded_detector_blind_spots.py -q
    

    Result: 1 failed, 3 passed — the one failure is the detector-blind-spot pin above; the passing control confirms the row is genuinely dispatchable, so this is not a malformed-row artifact.

    Operator impact: exactly as the issue states. After a queue move (or any re-pend) leaves an actor's stored assignment pointing at a queue with no live consumers, the affected jobs sit pending, due, forever — no deadline can fail them without schedule_to_close, and the one monitoring surface built to catch "no worker serves this" (taskq.jobs.stranded) publishes an empty gauge. The operator sees a healthy queue label, a pending job, and a fleet that visibly serves that label, with no way to discover the routing decision uses a different value.

    Fix direction: the stranded-jobs predicate needs the same branch the dispatcher's routing contract already encodes — test actor_config.queue (joined via j.actor) against workers.queues for rows with started_at IS NOT NULL, and j.queue for rows with started_at IS NULL, mirroring _dispatch_sql.py's "Routing contract" comment (src/taskq/backend/_dispatch_sql.py:76-90) and its two disjoint probe arms (jobs_actor_dispatch_idx/jobs_round_robin_probe_idx for never-claimed rows, jobs_repended_probe_idx for re-pended rows) as the precedent for keeping the two populations' predicates disjoint rather than merged into one incorrect OR.

    Relationship to #187 confirmed as stated: #187's fix (dispatch-side assignment routing for re-pended rows) is what created this exact divergence between the label and the effective routing queue; this issue is the general case — any re-pend, not only a queue move, produces it.

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

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions