Skip to content

app/: processing holds no long transaction and no blob locks; ProcessBuild timeout fits big builds - #118

Merged
ndbroadbent merged 1 commit into
mainfrom
app-processing-short-transactions
Dec 3, 2026
Merged

ndbroadbent merged 1 commit into
mainfrom
app-processing-short-transactions

Conversation

@ndbroadbent

@ndbroadbent ndbroadbent commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Production evidence (3 Dec 2026, first day on app/)

The first real CI upload failed: POST /api/v1/builds/…/snapshots returned 503 after 30 s. Postgres's lock log showed the request's INSERT INTO blobs … ON CONFLICT (sha256) DO UPDATE (blobs::register) waiting 29.8 s on a ShareLock held by the builds.process job. The job let go only when the worker killed it ("the job ran longer than its timeout of 1m"). ProcessBuild::handle ran all of processing::process inside one db::transaction with pg_advisory_xact_lock. That covered storage verification, renders, downloading, decoding and diffing images in compare, diff overlays through blobs::put, carry-forward, groups and the conclusion, so every blob row it wrote stayed locked for the whole run.

What changed

  • processing::exclusive(build_id, wait, work): a per-build lock in the cache store (laravel::cache::lock, Postgres in production). It's held for at most 20 min and released when work ends, fails or is dropped (Laravel's Lock::get releases on drop). It is taken and released in statements of its own, so work runs outside any transaction: storage requests, decoding and diffing hold no DB connection and no row locks. Blob writes (confirming uploads, the cached size and fingerprint, diff-overlay registration) and per-snapshot results each commit on their own straight away.
  • processing::run (what ProcessBuild::handle now calls) works in three steps:
    1. Charge in charge's own short transaction.
    2. stop_if_out_of_credits, outside any transaction as before.
    3. process under exclusive.
  • The conclusion is one short, database-only transaction at the end of process. It takes processing::lock (the advisory lock, which reviews also take), re-checks state == "processing", then charges, counts, runs source_unchanged, writes the warnings, concludes and records the auto-approval baselines, all or nothing.
  • charge is now its own short transaction under the advisory lock. An early comparison's charge and a run's charge can no longer interleave now that neither sits inside a long transaction. The conclusion calls the locked body directly.
  • compare_now (POST …/compare) and the demo reset use exclusive too, with no surrounding transaction. If the early comparison waits more than 20 s for the build, it answers 409 "ask again" instead of hanging until a 503.
  • ProcessBuild gets timeout = 15.minutes(), inside the lock's 20 minutes.
  • carry_forward: a rerun after a run killed part-way now compares a carried row that run inserted but left pending. Before, it skipped the row because it already existed. That couldn't happen while everything rolled back together.
  • Some futures are boxed (Box::pin). Without that, a few debug-build tests overflowed the stack, because the sync test queue runs the job inside the request that dispatched it.
  • docs/design-storage.md describes the new design.

Why two workers can't collide

I picked a claim (option b), but it needs no migration because the claim lives in the cache store's lock table (cache_locks). Option (a) would mean showing that every write is safe when two runs do it at the same time (page rows, diffs and carried rows written concurrently), not just when one run repeats them. That is harder to prove than keeping one run per build.

  • One run at a time. Only one run, early comparison or demo reset holds a build at a time, and all of them go through exclusive.
  • No lost wake-ups. A job that can't get the lock within 10 s doesn't tie up a worker. It leaves a note (builds.process-again:<id>), then tries the lock once more. Either it gets the lock, or whoever holds it finds the note when it releases and queues ProcessBuild again. So a render that finishes during a long run is never missed.
  • A killed worker. Its lock is released when the future is dropped. In the worst case it expires after 20 min, and the sweep's restart picks the build up again.
  • Reviews. They still serialize with the conclusion through the advisory lock. Nothing holds the advisory lock beyond a short transaction any more.
  • Reruns. Every per-snapshot write is safe to repeat in a later run:
    • compare overwrites a snapshot's result;
    • find_or_queue is keyed;
    • the removed and carried inserts use ON CONFLICT DO NOTHING, plus the pending-row fix above;
    • record_sources upserts;
    • the visualci.yml item is deleted and rebuilt at the start of every run;
    • charge charges only the difference from what was already charged.

The one behaviour change: a run that fails part-way leaves its per-snapshot writes in place instead of rolling them back. The build is marked failed either way.

Test

an_upload_registering_a_blob_does_not_wait_for_a_build_processing_it (app/tests/processing.rs):

  1. Registers a screenshot without uploading it and sets the build to processing.
  2. Runs processing::run against a blob store that accepts connections and never answers, so processing waits on the store at confirm_uploads.
  3. Meanwhile, blobs::register of the same blob runs with a 2 s timeout, and the test asserts it finishes.
  • Against main, it fails (registering the blob waited for processing: Elapsed(())). I checked this on a copy of main with a processing::run shim that is exactly the old handle body.
  • With the fix, it passes.

Results on agents-build-2: cargo fmt --check, cargo clippy --locked --all-targets -D warnings and cargo test --locked --no-fail-fast all pass, with nothing failing. The known flake invite_member_checks_who_may_invite_and_its_limits passed this time; it failed once in an earlier run, as #112 describes. The goldens are unchanged.


Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@ndbroadbent
ndbroadbent merged commit db151d5 into main Dec 3, 2026
11 checks passed
@ndbroadbent
ndbroadbent deleted the app-processing-short-transactions branch December 3, 2026 05:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant