← Back to Pull requests

[FIX] queue_job: apply max_retries to PostgreSQL concurrency errors #1009

Open
unknown opened this pull request with 367 lines diff across 4 files
View on GitHub
unknown unknown commented on Oct 13, 2026 09:46 UTC
Author

The problem

The typical PostgreSQL transaction serialization errors are retried, but the conversion from OperationalError to RetryableJobError happens in the runners, outside Job.perform() — and perform() is the only place max_retries is consulted.

  • queue_job/controllers/main.py wraps _try_perform_job and raises RetryableJobError from there; the surrounding except RetryableJobError calls retry_postpone(), which writes a new eta and requeues.

  • queue_job_cron_jobrunner/models/queue_job.py does the same inline, calling postpone() + set_pending(reset_retry=False) directly.
  • Neither consults max_retries. perform() has already incremented retry by the time the error surfaces, so a job that keeps colliding is retried for ever while its counter climbs past the limit, and it never reaches failed.

    Seen in production on a long report-building job: it sat at try 10 of a maximum of 5, each attempt running for about eight minutes, until it was closed by hand.

    The change

    Convert the error inside Job.perform() instead, so it falls into the RetryableJobError handling already there and is subject to max_retries like any other retryable error:

    self.retry += 1
    try:
        try:
            ...
            self.result = self.func(*tuple(self.args), **self.kwargs)
        except OperationalError as err:
            if err.pgcode not in PG_CONCURRENCY_ERRORS_TO_RETRY:
                raise
            raise RetryableJobError(
                ustr(err.pgerror, errors="replace"), seconds=PG_RETRY
            ) from err
    except RetryableJobError as err:
        ...  # unchanged: ignore_retry, infinite retries, max_retries
    

    The duplicated handling then comes out of both runners, and PG_RETRY moves beside DEFAULT_MAX_RETRIES in job.py. Net effect is a deletion: one conversion in one place, and both runners behave identically.

    _enqueue_dependent_jobs keeps its own OperationalError handling — that is a separate retry loop around the dependency update, not around the job.

    Behaviour

    | | before | after |
    |---|---|---|
    | serialization failure, under max_retries | postponed | postponed, unchanged |
    | serialization failure, at max_retries | postponed for ever | FailedJobError, job fails |
    | any other OperationalError | fails the job | fails the job, unchanged |
    | max_retries = 0 | infinite | infinite, unchanged |

    Tests

    test_queue_job gains a raise_pg_concurrency option on testing_method, backed by a small OperationalError subclass — psycopg2 exposes pgcode read-only and errors.lookup() leaves it unset, so the code has to be carried that way. Two cases in test_job.py, mirroring the existing test_retryable_error:

  • a serialization failure retries and gives up at max_retries

  • any other OperationalError is not retried
  • Full suite on 19.0: 186 tests, 0 failed, 0 errors (queue_job, test_queue_job, queue_job_cron_jobrunner).

    Diff: 367 lines (+275 / -92) across 4 files Reviewer: RicardCForgeFlow Review duration: 70424m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.50) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 367 lines diff across 4 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    44%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.353
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #1009"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 44.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 49.7% (MEDIUM) · Higher = larger blast radius
    49.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 44.7% · Lower = rushed or shallow review
    44.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.5% · Safe to merge
    27.5%
    Repository