Skip to content

ack_orchestration_item validates the lock without a row lock; a concurrent fetch can leave consumed messages in the queue #29

Closed

Description

@affandar

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:
    -- 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:
    -- 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:
    -- 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

  1. Turn A holds token T1. Its lock is at the point of expiry, and no one else has taken the instance yet.
  2. A calls ack_orchestration_item. p_now_ms is A's clock at the time it built the call. Step 1 passes if locked_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.
  3. 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 into instance_locks and into the queued messages, and commits. A holds no lock that would make B wait.
  4. A reaches Step 11. DELETE ... WHERE lock_token = T1 matches 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.
  5. 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 UPDATE on the instance_locks row, or take the same advisory lock as fetch_orchestration_item. Compare locked_until with the database clock.
  • Step 12: read the row count after the delete and raise Invalid lock token when it is 0. The whole commit then rolls back if the lock changed hands during the transaction.

Related

Tracked in microsoft/duroxide#55.

Activity

  1. added
    bug
    on Oct 3, 2026
  2. waldemort-auto commented on Oct 26, 2026

    @waldemort-auto
    Contributor

    Reproduced on PostgreSQL 15 and fixed in #44.

    Reproduction

    • A test trigger pauses the acknowledge right after its token check.
    • A fetch from a worker whose clock runs ahead takes the instance over and commits.
    • The acknowledge then resumes, deletes 0 rows and commits.
    • End to end with duroxide 0.1.32, one enqueued queue message was charged twice, in 4 of 4 runs.
    • The same happens with no clock skew when the lease simply expires during the acknowledge.

    Fix (migration 0026 in #44)

    • The acknowledge takes the per-instance advisory lock that fetch takes, right after its token check.
    • It then checks the token again.
    • It raises if deleting its instance lock removes no row, so the whole turn rolls back.

    Tests

    • tests/deadlocks/ack_fetch.rs
    • tests/orchestration_ack_lease_tests.rs, including an end-to-end runtime test that charges an order once.

    Merging #44 closes this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions