Skip to content

agent: prevent delivery deadlocks with connection deletion on PostgreSQL - #2104

Open
evgeny-simplex wants to merge 3 commits into
simplex-chat:masterfrom
evgeny-simplex:ep/delivery-conn-lock
Open

evgeny-simplex wants to merge 3 commits into
simplex-chat:masterfrom
evgeny-simplex:ep/delivery-conn-lock

Conversation

@evgeny-simplex

@evgeny-simplex evgeny-simplex commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

On PostgreSQL, a message delivery worker locks the connection row before it deletes the delivery of a sent message. A connection deletion locks that row first too, so the later of the two transactions waits until the earlier one commits. A delivery deletion also waits while a send batch that includes the connection is being committed, as receiving already does.

Before this change, PostgreSQL aborted one of the two transactions with 40P01 "deadlock detected" after a 1-second wait:

  • When the connection deletion was aborted, the deleted user's connection was left in place until the cleanup manager deleted it, up to 5 minutes later. DEL_USER came only then.
  • When the delivery deletion was aborted, the worker restarted and reported INTERNAL "Worker msg_delivery ... terminated".

Agent store:

  • deleteSndMsgDelivery calls lockConnForUpdate first. The message receive transaction already does this (agentClientMsg). On SQLite, lockConnForUpdate does nothing.

Tests:

The deadlock in the simplex-chat test "multiple users, delete user" on PostgreSQL, from the server log of a run of master with tracing (https://github.com/evgeny-simplex/simplex-chat/actions/runs/45599791712/job/139155524461):

  • DELETE FROM connections WHERE conn_id = 'hTolAXddQQxTTWNX' waits "while deleting tuple (0,14) in relation snd_message_deliveries".
  • DELETE FROM messages WHERE conn_id = 'hTolAXddQQxTTWNX' AND internal_id = 14 (the delivery deletion) waits for the connection deletion.
  • PostgreSQL aborted the connection deletion, and the test failed with "no output for 20 seconds, expected: completed deleting user".
  • In another copy of the test in the same job, PostgreSQL aborted the delivery deletion, and the test got "internal error: Worker msg_delivery ... 40P01 ... DELETE FROM messages" in place of "no users".

The same failures elsewhere:

Without this commit, 3 of 7 traced simplex-chat runs (16 extra copies of the test each) showed this deadlock in the test: a 40P01 abort, or a 1-second wait of the connection deletion. With this commit, the same runs had no 40P01 in the PostgreSQL server log and no failures:

simplex-chat master with only this commit, all PostgreSQL tests pass: https://github.com/evgeny-simplex/simplex-chat/actions/runs/45601320290/job/139166857183

/Connection deletion/
/Work item reads/
/Core tests/Encoding tests/

@evgeny-simplex
evgeny-simplex marked this pull request as ready for review November 25, 2026 13:37

This branch has not been deployed

No deployments
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