Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This is a **PostgreSQL provider** for [Duroxide](https://github.com/microsoft/du

### Key Design Decisions
1. **Stored Procedures over inline SQL** - All database operations use schema-qualified stored procedures in `0002_create_stored_procedures.sql` for atomic transactions and deadlock prevention
2. **Rust-provided timestamps** - All procedures receive `p_now_ms` from Rust (not database `NOW()`) to avoid clock skew (see migration `0006`). Exception: `fetch_orchestration_item` computes the lease as `GREATEST(p_now_ms + time spent in the call, clock_timestamp()) + timeout`, so a lease is never already expired when it is returned (migration `0025`)
2. **Rust-provided timestamps** - All procedures receive `p_now_ms` from Rust (not database `NOW()`) to avoid clock skew (see migration `0006`). Exceptions: `fetch_orchestration_item` computes the lease as `GREATEST(p_now_ms + time spent in the call, clock_timestamp()) + timeout`, so a lease is never already expired when it is returned (migration `0025`); `renew_orchestration_item_lock` uses the same clock after its advisory-lock wait to check expiry and compute the new deadline, and never moves a deadline earlier (migration `0026`)
3. **Schema isolation** - Multi-tenant support via custom PostgreSQL schemas (`new_with_schema()`)
4. **Two-phase locking in fetch operations** - Prevents B-tree index deadlocks (peek query → advisory lock → FOR UPDATE verification)

Expand Down Expand Up @@ -177,3 +177,4 @@ Use [.agents/skills/release-preparation/SKILL.md](../.agents/skills/release-prep
- Enable tracing: `RUST_LOG=duroxide::providers::postgres=debug cargo test`
- Check schema cleanup: Run `scripts/cleanup_test_schemas.sh` after failed tests
- Deadlock issues: Review two-phase locking in `fetch_orchestration_item` stored procedure
- Deadlock tests live in `tests/deadlocks` (`cargo test --test deadlocks`). They run in every default `cargo test` and must not be `#[ignore]`d. Every lock-ordering change adds a test there.
46 changes: 46 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,52 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- **Orchestration lock renewal no longer deadlocks with fetch or with
acknowledge and abandon, and no longer reports success for an expired lease
(migration `0026`).** Renewal takes the same per-instance transaction
advisory lock as `fetch_orchestration_item` before it locks any row, so the
two can no longer deadlock (SQLSTATE `40P01`). It then updates queue rows
before the instance lock, the order that acknowledge and abandon use. After
the advisory-lock wait, renewal reads the time again: it fails if the lease
expired during the wait, and it never moves a lease deadline earlier.
Because renewal can now be aborted as a deadlock victim while it waits for
that lock, the provider retries transient renewal errors up to three times,
as fetch and acknowledge already do. The migration also adds a unique index
on `instance_locks(lock_token)`, so renewal, acknowledge and abandon find an
instance by its lock token without scanning the table.
A new deadlock suite, `tests/deadlocks` (`cargo test --test deadlocks`),
covers the fetch deadlock and the acknowledge and abandon lock orders, and
checks that a renewal aborted as a deadlock victim is retried. Two of its
tests race renewal against a fetch from a node that sees the lease as
expired (clock ahead, or renewal stalled), with no instrumentation, and
require no deadlock and no revived expired lease. The suite runs in every
default `cargo test`. `tests/orchestration_lock_renewal_tests.rs` covers
each change that can happen between renewal's token lookup and its update:
time passing, lease expiry, a fetch takeover, acknowledge, abandon, and
another renewal, and checks the `lock_token` index.
- **An orchestration acknowledge can no longer commit after another worker
has taken the instance over, which processed messages twice (migration
`0026`).** Acknowledge checked its lock token once, without a lock. A
fetch from a worker whose clock runs ahead, or after a stalled acknowledge,
could take the instance over between that check and the commit. The
acknowledge then committed its history and new work, but its deletes,
which match on the old token, removed nothing, so the processed messages
stayed queued and were delivered again. Duroxide does not de-duplicate
queue messages or external events, so they were processed twice: one
enqueued event could run an activity twice. Acknowledge now takes the
per-instance advisory lock right after its token check, checks the token
again, and fails if its instance lock is gone when it deletes it.
`tests/deadlocks/ack_fetch.rs` checks that a fetch waits for an
acknowledge in progress. `tests/orchestration_ack_lease_tests.rs` checks a
takeover while acknowledge waits for the lock, a lock deleted during the
acknowledge, and, end to end with the duroxide runtime, that an order is
charged once when another worker fetches during the acknowledge.
`docs/tla/orchestration-leases` contains a TLA+ model of fetch,
acknowledge, renew and abandon under worker clock skew. Its TLC
configurations and expected results run with `./run.sh`.

## [0.1.37] - 2026-10-21

### Fixed
Expand Down
3 changes: 3 additions & 0 deletions docs/tla/orchestration-leases/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
logs/
states/
tla2tools.jar
Loading
Loading