Repository navigation
fix(api): server delete takes locks in a deadlock-free order - #46
Merged
Merged
Conversation
Deleting a server locked the servers row first and then wrote agent_certificates and secrets, the reverse of certificate sync and the credential PATCH, so a concurrent renewal could deadlock it. The delete now locks secrets, then agent_certificates, then servers, re-checks the credential once the locks are held, retries one deadlock or serialization failure and answers 409 server.busy on a second. Every lock query is scoped to the caller's organization, so another org's server id takes no lock. Certificate sync also retries when it is chosen as a deadlock victim.
…n string The deadlock and cross-org lock tests read the connection string back from an opened DbContext, which Npgsql returns without the password. CI's Postgres requires one, so both tests failed to connect there; use the test factory's connection string instead.
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.
What and why
Follow-up to #45. That fix made
DELETE /servers/{id}lock theserversrow first, then writeagent_certificates(revoke) andsecrets(credential release) on save. Certificate sync after a renewal (AgentSessionHandler.SyncCertificatesAsync) and the PATCH that changessshCredentialSecretIdtake those locks the other way round, so a renewed agent reconnecting while its server is deleted could deadlock: PostgreSQL aborts one side with 40P01, which surfaced as a 500 on the delete or an unhandled error in the sync.Changes:
server.busy.organization_id, so a DELETE with another org's server id returns 404 without locking that row (before, it briefly stalled that server's heartbeat writes).No migration, proto or OpenAPI changes.
How I verified it
With
AETHERA_TEST_DBset (Postgres 17):dotnet build -warnaserror: 0 warnings, 0 errors.AgentApiTests|Servers): 62 passed, 1 skipped (SSH integration), 0 failed.StaticWebTestshas two failures locally that also fail onmaster: Windows path assertions (D:\repo\...vs/repo/...), unrelated. The full suite on Windows is noisy from ephemeral-port exhaustion; CI is the clean signal.Checklist
dotnet build -warnaserror && dotnet test(withAETHERA_TEST_DBset),go vet ./... && go test ./...,pnpm lint && pnpm test && pnpm buildCHANGELOG.mdupdated under Unreleased if users or operators would notice (not updated, same as fix(api): deleting a server no longer races its agent's writes #45: a rare race)