Messaging: one global lock order (fixes a deadlock) #95

Merged
calvin merged 1 commit from messaging-lock-order into master 2026-11-13 07:07:25 +00:00
Owner

Router.advance locked the request, then the connector (the command outbox). Inbound.process locked the connector, then the message, then the request. A status event and the next attempt of the same request on the same remote connector could deadlock; Postgres aborts one side.

Fix: one order everywhere, documented in Karta.Messaging.Locks and Karta.Messaging: connector, command, request, message. advance locks the request's remote connectors first; Attempts, receipt_check, ProviderEvents and Inbound take request before message. Locks.retry reruns top-level work (Inbound, ProviderEvents, advance, Commands.finished) if Postgres still aborts one.

Tests (lock_order_test.exs, real connections, no retry allowed): the two paths run against each other through the before_attempt hook; the mutation check (removing the connector pre-lock) gives 40P01. Also a forced deadlock that is retried and completes, and a non-deadlock error that is not retried.

Bird echo: Bird's documentation says events echo the send's tags and metadata, so matching an early report by data.metadata.karta_message_id rests on documentation, not on a seen report. It is not a new assumption, but the acceptance run must confirm it (added to the new "Before switching on" checklist in docs/suite/release-1.0/karta-messaging.md).

🤖 Generated with Claude Code

`Router.advance` locked the request, then the connector (the command outbox). `Inbound.process` locked the connector, then the message, then the request. A status event and the next attempt of the same request on the same remote connector could deadlock; Postgres aborts one side. Fix: one order everywhere, documented in `Karta.Messaging.Locks` and `Karta.Messaging`: connector, command, request, message. `advance` locks the request's remote connectors first; `Attempts`, `receipt_check`, `ProviderEvents` and `Inbound` take request before message. `Locks.retry` reruns top-level work (Inbound, ProviderEvents, advance, Commands.finished) if Postgres still aborts one. Tests (`lock_order_test.exs`, real connections, no retry allowed): the two paths run against each other through the `before_attempt` hook; the mutation check (removing the connector pre-lock) gives `40P01`. Also a forced deadlock that is retried and completes, and a non-deadlock error that is not retried. Bird echo: Bird's documentation says events echo the send's `tags` and `metadata`, so matching an early report by `data.metadata.karta_message_id` rests on documentation, not on a seen report. It is not a new assumption, but the acceptance run must confirm it (added to the new "Before switching on" checklist in `docs/suite/release-1.0/karta-messaging.md`). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Messaging: one global lock order (connector, command, request, message)
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
eaffc12767
advance locked request then connector (via the outbox); Inbound locked
connector, message, then request: a status event and the next attempt of the
same request on the same remote connector could deadlock. Every path now
follows Karta.Messaging.Locks; Locks.retry is the net for the rest.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
calvin merged commit 1fb40b40f9 into master 2026-11-13 07:07:25 +00:00
calvin deleted branch messaging-lock-order 2026-11-13 07:07:25 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
external/karta!95
No description provided.