Repository navigation
Description
Summary
ack_orchestration_item validates the lock token once, at the start, with a plain SELECT. It compares the lock with a timestamp that the caller computed before the call. It does not lock the instance_locks row, and it does not check that its last two DELETEs removed anything.
If another dispatcher fetches the same instance while the commit runs, the commit can succeed while the messages that it consumed stay in the queue. Those messages are then processed a second time.
Where
migrations/0020_add_kv_delta.sql:
- Step 1, the lock check:
duroxide-pg/migrations/0020_add_kv_delta.sql
Lines 73 to 80 in ba15e13
-- Step 1: Validate lock token SELECT il.instance_id INTO v_instance_id FROM %1$I.instance_locks il WHERE il.lock_token = p_lock_token AND il.locked_until > p_now_ms; IF NOT FOUND THEN RAISE EXCEPTION 'Invalid lock token'; END IF; - Step 11, delete the locked messages:
duroxide-pg/migrations/0020_add_kv_delta.sql
Lines 294 to 295 in ba15e13
-- Step 11: Delete locked messages DELETE FROM %1$I.orchestrator_queue q WHERE q.lock_token = p_lock_token; - Step 12, remove the instance lock:
duroxide-pg/migrations/0020_add_kv_delta.sql
Lines 297 to 299 in ba15e13
-- Step 12: Remove instance lock DELETE FROM %1$I.instance_locks il WHERE il.instance_id = v_instance_id AND il.lock_token = p_lock_token;
How it can fail
- Turn A holds token T1. Its lock is at the point of expiry, and no one else has taken the instance yet.
- A calls
ack_orchestration_item.p_now_msis A's clock at the time it built the call. Step 1 passes iflocked_until > p_now_ms. Any delay between building the call and running Step 1 makes the check too generous. Examples are a slow connection checkout and a stalled process. - While A's transaction runs steps 2 to 10, dispatcher B calls
fetch_orchestration_item. For B the lock is expired. B writes its token T2 intoinstance_locksand into the queued messages, and commits. A holds no lock that would make B wait. - A reaches Step 11.
DELETE ... WHERE lock_token = T1matches 0 rows, because the rows now carry T2. Step 12 matches 0 rows as well. Nothing checks this. A commits its history, its new work items and its KV changes. - The messages that A consumed are still queued and locked by B. B replays them on the history it read before A's commit, and its own commit fails on the history primary key. After B's lock expires, the next fetch returns A's history together with the same messages.
A completion message is matched by its event ID, so a second delivery is ignored. An external event has no such ID. It can be applied twice.
How this was checked
Found by reading the code. Not reproduced.
Suggested fix
- Step 1: use
SELECT ... FOR UPDATEon theinstance_locksrow, or take the same advisory lock asfetch_orchestration_item. Comparelocked_untilwith the database clock. - Step 12: read the row count after the delete and raise
Invalid lock tokenwhen it is 0. The whole commit then rolls back if the lock changed hands during the transaction.
Related
- fetch_orchestration_item writes an already-expired lock lease when the advisory lock wait exceeds the lock timeout #21: the fetch side writes a lease from a stale
p_now_ms.
Tracked in microsoft/duroxide#55.
Activity
waldemort-auto commentedon Oct 26, 2026
Reproduced on PostgreSQL 15 and fixed in #44.
Reproduction
Fix (migration
0026in #44)Tests
tests/deadlocks/ack_fetch.rstests/orchestration_ack_lease_tests.rs, including an end-to-end runtime test that charges an order once.Merging #44 closes this issue.