Skip to content

fix(worker): runtime orphan recovery for stuck OCR jobs #129

Description

@kvithayathil

Problem

When the worker process dies mid-execution (after setting job status to OCR_STARTED but before the OpenAI batch is submitted), the job is permanently stuck:

  1. provider_job_id remains NULL — no remote batch to poll
  2. Worker only picks up NOT_STARTED jobs (worker.py:174)
  3. Orphan detection only runs at startup (_detect_orphaned_jobs in worker.py:159) — if the server stays up, the stuck job is never recovered
  4. The orphan detector terminates stuck jobs to error states rather than retrying them

Reproduction

  1. Start a job with a real OpenAI provider
  2. Kill the worker process after job transitions to OCR_STARTED but before batch submission completes
  3. If server stays up: job stuck forever in OCR_STARTED
  4. If server restarts: orphan detector moves it to OCR_FAILED (data lost, no retry)

Impact

  • Jobs can get stuck in OCR_STARTED, OCR_COMPLETED, MATCHING_PENDING, or MATCHING states with no recovery path while the server is running
  • The only recovery is a server restart, which terminates the job to an error state rather than retrying

Suggested Fix

Add periodic runtime orphan detection to the worker loop — every N seconds check for jobs stuck in non-terminal states beyond a threshold, and either retry (reset to NOT_STARTED) or terminate gracefully.

Alternatively, add a POST /api/jobs/{id}/retry endpoint that resets a stuck job.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:backendBackend (Python/FastAPI) changesbugSomething isn't workingpriority:mediumNormal priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions