Skip to content

feat(task-queue): capped stalled-job recovery as a supervised ops loop - #590

Closed
aivong-openhands wants to merge 1 commit into
task-queue/04-watchdogfrom
task-queue/05-recovery
Closed

aivong-openhands wants to merge 1 commit into
task-queue/04-watchdogfrom
task-queue/05-recovery

Conversation

@aivong-openhands

@aivong-openhands aivong-openhands commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Stack 5/7: OHE-3417 Track 1. Based on #589. Implements T1.6 (retry_stalled_jobs and remove_old_jobs) and T1.7.1.

Why not a Procrastinate task

procrastinate_fetch_job_v2 rejects a candidate whose lock matches a job already doing. If recovery were a locked periodic task and its worker died mid-run, every later occurrence would be unfetchable, and the only thing that could clear it is the task that can no longer run. Recovery would deadlock itself. Instead, each ops worker runs it as a timer thread with a dedicated connection, with no queue row and no execution lock.

Design

  • One bounded transaction per pass under pg_try_advisory_xact_lock. Transaction-scoped, so replicas coordinate under every pooling mode, including PgBouncer transaction pooling. A busy lock skips the pass. Passes are batch-limited.
  • Only Class A queues (business, ops) are recovery candidates, and only when the worker's heartbeat is older than stalled_worker_timeout or its row was pruned. Class B work (Track 2) is excluded by filter, not convention.
  • Revalidation inside the mutating transaction. Each candidate is re-read FOR UPDATE and must still have the same worker_id, the same attempts, and a stale worker. retry_job_v2 checks neither worker nor attempt, so acting on a stale selection could requeue a healthy execution.
  • Capped at MAX_ATTEMPTS. The last attempt is failed, not retried, so a poison job cannot restart a worker every minute forever.
  • Each pass registers a deadline with the watchdog from feat(task-queue): local watchdog with bounded self-exit #589. A pass hung on a black-holed socket, with a healthy event loop and an open DB session, still ends in a bounded self-exit.
  • Retention deletes final-state jobs older than TASK_QUEUE_RETENTION_HOURS (168) in batches. It skips jobs still referenced by procrastinate_periodic_defers, whose FK has no ON DELETE.

Tests

Real Postgres. The recovery unit suite covers:

  • #16a: recovery resumes after the ops worker is killed mid-pass.
  • #16e: a hung pass ends in exit.
  • #16f: a job reassigned between selection and mutation is dropped. So is a worker that resumed after selection: same identity, fresh heartbeat. This is the liveness check; it was mutation-tested, and each revalidation clause has a test that fails without it.
  • #16g: no advisory lock is held between passes, and a failed pass closes its connection.
  • The retry cap (build(deps): bump docker/setup-buildx-action from 3.12.0 to 4.2.0 in /.github/actions/docker-merge-manifest #9) and retention.

Also an end-to-end process test (#13): a frozen business worker self-exits, a separate ops process recovers the job, and a fresh business worker completes it.


This PR was created by an AI agent (OpenHands) on behalf of the user.

@aivong-openhands can click here to continue refining the PR


Enterprise server image for this PR:

ghcr.io/openhands/enterprise-server:sha-af1314e

@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  server/task_queue
  config.py 123, 129
  maintenance.py 271-277
  worker.py
Project Total  

This report was generated by python-coverage-comment-action

@aivong-openhands
aivong-openhands force-pushed the task-queue/04-watchdog branch 2 times, most recently from a22c7d3 to 5092d31 Compare September 30, 2026 18:44
Recovery is not a Procrastinate task: a locked recovery task whose worker
died would block every later occurrence of itself. Each ops worker runs
it on a thread with a dedicated connection instead.

- One bounded transaction per pass under pg_try_advisory_xact_lock, so
  replicas coordinate under every pooling mode; a busy lock skips.
- Candidates are Class A queues only (business, ops) whose worker's
  heartbeat is older than stalled_worker_timeout, or whose worker row was
  pruned.
- Each candidate is re-read FOR UPDATE and revalidated (same worker_id,
  same attempts, worker still stale) before retry_job_v2 or finish_job:
  retry_job_v2 checks neither worker nor attempt, so a stale selection
  could requeue a healthy execution.
- Capped at MAX_ATTEMPTS executions; the last is failed, not retried.
- Each pass registers a deadline with the watchdog, so a pass hung on a
  black-holed socket still ends in a bounded self-exit.
- Retention deletes final-state jobs past TASK_QUEUE_RETENTION_HOURS in
  batches, skipping jobs a periodic_defers row still references.

OHE-3417 (Track 1, T1.6, T1.7.1)

Co-authored-by: openhands <openhands@all-hands.dev>
@aivong-openhands

aivong-openhands commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Closing this in favour of #602, which does the same job out of the box.

maintenance.py here is 277 lines doing what job_manager.get_stalled_jobs(), job_manager.retry_job() and builtin_tasks.remove_old_jobs already provide, and it does it by calling procrastinate_retry_job_v2 and procrastinate_finish_job_v1 as raw SQL. Those are internal to the schema, so this would also need revisiting on every Procrastinate upgrade — a maintenance cost that buys nothing over the library's own API.

The design argument in the PR description doesn't hold either. I had it that recovery can't be a Procrastinate task, because the job would stay doing after its worker died and later occurrences could never be fetched. That is true of an execution lock, but not of a queueing_lock, whose unique index is WHERE status='todo': the next occurrence queues normally, a healthy worker picks it up, and get_stalled_jobs recovers the previous one. Recovery recovers the recoverer, with no advisory lock and no supervised loop. #602 takes that approach and is the better design.

One gap I'll raise on #602 separately: retry_stalled_jobs there retries unconditionally, with no check on job.attempts. A job that reliably freezes its worker is re-queued every 10 minutes indefinitely, taking a worker down each time. Capping on attempts and finishing past the cap turns that into a single alert rather than a loop. That is a few lines in the housekeeping task, not a reason to keep anything in this PR.

I'm re-examining #589 (the local watchdog) on the same grounds. Its premise is that a hung job keeps the heartbeat alive so stalled detection never fires, but that only holds for an async job awaiting something that never returns, and a per-task asyncio.timeout covers that case without a watchdog thread. If the event loop genuinely freezes, the heartbeat stops with it and ordinary stalled recovery applies. The remaining justification was Docker Compose, which isn't built yet. I'll post findings there rather than assume it survives.

Net effect on the stack: this PR closes, #591 and #592 rebase onto #589, and the worker in #588 uses the housekeeping from #602 instead of its own.

This comment was drafted by an AI agent (OpenHands) on behalf of @aivong.

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

Labels

type: feat A new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants