Repository navigation
agent: prevent delivery deadlocks with connection deletion on PostgreSQL - #2104
Open
evgeny-simplex wants to merge 3 commits into
Open
evgeny-simplex wants to merge 3 commits into
evgeny-simplex wants to merge 3 commits into
Conversation
evgeny-simplex
marked this pull request as ready for review
November 25, 2026 13:37
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
Agent store:
deleteSndMsgDeliverycallslockConnForUpdatefirst. The message receive transaction already does this (agentClientMsg). On SQLite,lockConnForUpdatedoes nothing.Tests:
snd_messagesrow. While that lock is held, the delivery deletion waits on the row, the connection deletion starts, and then the lock is released. Both transactions must complete.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.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