Repository navigation
fix: serialize orchestration lock renewal with fetch - #43
waldemort-auto[bot] wants to merge 5 commits into
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
| .await | ||
| }); | ||
|
|
||
| wait_for_advisory_holder(&pool, ready_key).await; |
There was a problem hiding this comment.
Could we add a separate regression for renewal waiting while a real fetch replaces its token? This added regression reaches line 192 after renewal holds the production instance advisory key, so fetch waits instead; it does not exercise ownership changing during renewal's wait.
Head's guard is correct; this is regression protection, not a current defect. In prior disposable PostgreSQL 15 READ COMMITTED checks, removing only AND lock_token = p_lock_token at migrations/0026_serialize_orchestration_lock_mutations.sql:49 left this added regression passing 3/3. A separate wait-then-replace schedule made that mutant overwrite the replacement instance lease, whereas intact head rejected renewal with the intended P0001 and preserved the replacement lease.
Suggested schedule:
Seed a live old_token with a 30-second lease.
Holder: BEGIN; pg_advisory_xact_lock(hashtext(instance_id)).
Renewal: renew_orchestration_item_lock(old_token, renew_now, 60).
Wait for pg_locks / pg_blocking_pids to identify Holder as its advisory blocker.
Holder: fetch_orchestration_item(renew_now + 120000, 30000, NULL, NULL).
Snapshot the replacement token and leases in nonempty rows of both tables;
COMMIT Holder, then require renewal's P0001:
"Lock token invalid, expired, or already released".
Assert instance_locks and orchestrator_queue still match that snapshot.
The future caller timestamp makes reclamation deterministic without a wall-clock sleep. The token-predicate mutant would fail the replacement-state assertion. Equivalent existing coverage of this exact blocked-renewal/replacement schedule would satisfy the request; upstream provider-validation bodies were not inspected or run, so their coverage is unknown.
There was a problem hiding this comment.
Superseded by #44, which contains every commit of this PR unchanged, followed by the follow-up fixes and tests.
Thank you, Andrew Chung, for finding and reproducing the fetch/renew lock-order deadlock and for the advisory-lock serialization that #44 builds on. Thank you, ChrisKrawczyk, for the review: your blocked-renewal/takeover schedule is now toctou_fetch_takes_over_while_renewal_waits in tests/orchestration_lock_renewal_tests.rs.
#44 also:
- reads the time after renewal's advisory-lock wait, and never shortens a lease;
- fixes renewal's row order against acknowledge and abandon;
- fences acknowledge against a takeover during the acknowledge. Without that, one enqueued event could be processed twice;
- retries transient renewal errors;
- adds a
lock_tokenindex; - adds a deadlock suite that runs in every default
cargo test.
Closing in favour of #44.
Summary
Fixes the PostgreSQL fetch/renew lock-order deadlock and keeps a deterministic regression test.
Migration
0026resolves the instance for a live renewal token, then acquires the same transaction-scoped per-instance advisory lock thatfetch_orchestration_itemalready uses. Renewal revalidates the token after any advisory-lock wait, then updatesinstance_locksandorchestrator_queue.Why this fixes the deadlock
Before this change, fetch could lock
orchestrator_queueand wait forinstance_lockswhile renewal heldinstance_locksand waited for the queue. The common advisory lock serializes fetch and renewal before either procedure takes those conflicting row locks.Reordering renewal's two
UPDATEs alone is insufficient with multiple queue messages, so this PR uses the common per-instance serialization point instead.Regression coverage
The PostgreSQL integration test uses per-run advisory keys and an explicitly released gate. It confirms fetch waits while renewal holds the serialization path, then requires both operations to finish without SQLSTATE
40P01.Validation
cargo test --test fetch_renew_lock_order_repro_tests -- --nocaptureagainst disposable PostgreSQL 15: 1 passedcargo fmt --checkbecause the pinned Rust toolchain did not includecargo-fmt:error: 'cargo-fmt' is not installed for the toolchain '1.97.1-x86_64-unknown-linux-gnu'.