Repository navigation
fix(api): deleting a server no longer races its agent's writes - #45
Merged
Merged
Conversation
A connected agent persists heartbeats and status on the server row at any
moment. When one landed between the delete's read and its save, the xmin
check failed and DELETE /servers/{id} answered 409 to a client that sent
no If-Match (seen in CI as a flaky AgentApiTests failure).
The delete now locks the server row (SELECT ... FOR UPDATE) in a
transaction before reading it, so the agent's writes wait for the commit.
If-Match is checked against the locked version, so a stale ETag still
gets 412.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 of 6 tasks
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.
DELETE /api/v1/servers/{id}could answer 409 Conflict to a request that sent noIf-Matchwhen the server's agent was connected. The agent persists heartbeats and status on the server row at any moment. A write between the delete's read and its save changed the row version (xmin), so the save failed the concurrency check. It showed up in CI as a flakyDeleting_a_server_revokes_its_certificates_and_closes_its_live_stream.Fix: the delete locks the server row with
SELECT … FOR UPDATEin a transaction before reading it. The agent's writes wait the few milliseconds until the commit, andIf-Matchis checked against the locked version, so a stale ETag still gets 412.I tried an optimistic retry first, but it starves under continuous writes (still 409 after 5 attempts), hence the lock.
Tests (
AgentApiTests):Deleting_a_server_succeeds_while_its_agent_keeps_writing_the_row: a background writer updates the row non-stop during 5 deletes. Fails on master with 409; passes with the fix, 5 out of 5 runs.Deleting_a_server_with_a_stale_if_match_is_still_refused: 412 and the server isn't deleted.Known edge: an agent reconnecting with a renewed certificate at the exact moment its server is deleted writes the certificate and server rows in the opposite order. Postgres would detect the deadlock and abort one of the two. That is rarer than the bug fixed here and resolves itself.
🤖 Generated with Claude Code