Skip to content

fix: serialize orchestration lock renewal with fetch - #43

Closed
waldemort-auto[bot] wants to merge 5 commits into
mainfrom
waldemort/repro-fetch-renew-lock-order
Closed

waldemort-auto[bot] wants to merge 5 commits into
mainfrom
waldemort/repro-fetch-renew-lock-order

Conversation

@waldemort-auto

@waldemort-auto waldemort-auto Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes the PostgreSQL fetch/renew lock-order deadlock and keeps a deterministic regression test.

Migration 0026 resolves the instance for a live renewal token, then acquires the same transaction-scoped per-instance advisory lock that fetch_orchestration_item already uses. Renewal revalidates the token after any advisory-lock wait, then updates instance_locks and orchestrator_queue.

Why this fixes the deadlock

Before this change, fetch could lock orchestrator_queue and wait for instance_locks while renewal held instance_locks and 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 -- --nocapture against disposable PostgreSQL 15: 1 passed
  • The T3 image could not run cargo fmt --check because the pinned Rust toolchain did not include cargo-fmt: error: 'cargo-fmt' is not installed for the toolchain '1.97.1-x86_64-unknown-linux-gnu'.

Waldemort Agent and others added 5 commits October 22, 2026 15:44
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>
@waldemort-auto waldemort-auto Bot changed the title test: reproduce fetch-renew orchestration lock-order deadlock fix: serialize orchestration lock renewal with fetch Oct 22, 2026
.await
});

wait_for_advisory_holder(&pool, ready_key).await;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@waldemort-auto waldemort-auto Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_token index;
  • adds a deadlock suite that runs in every default cargo test.

Closing in favour of #44.

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