fix(storage): surface a friendly error on a racing connection create - #6446
Merged
Merged
Conversation
ConnectionStorage.create() checks for an existing row, then inserts if none is found. That check-then-insert is TOCTOU-racy: two concurrent create() calls for the same id can both see no row and both attempt the insert. The primary-key constraint rejects the loser, but create() let that raw driver error (a 'duplicate key value violates unique constraint' Postgres error) escape unhandled, instead of the friendly 'Connection ID already exists' error createNew() already gives for the identical race. create() is a live path (reports/setup.ts, monitor-run-start.ts, plugin-config-update.ts, deco-sites.ts all call it with a deterministic, org-scoped id), so a double-submit or racing webhook can hit this today. Fix: catch the duplicate-key/unique-constraint error around the insert and rethrow the same friendly error createNew() uses.
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.
Source: bug found while auditing
apps/api/src/storage/connection.tsfor races/error handling per the storage-layer focus area.Why a maintainer wants this:
ConnectionStorage.create()does a check-then-insert (select for an existing row, insert if none found) that is TOCTOU-racy — two concurrentcreate()calls for the same id can both pass the existence check, then both attempt the insert. The primary-key constraint onconnections.idrejects the loser, butcreate()let that raw Postgres driver error (duplicate key value violates unique constraint ...) escape unhandled, instead of the friendly"Connection ID already exists"error that the siblingcreateNew()method already gives for the exact same race (see its existing test at line 90-115 of the integration test file).create()is a live path —reports/setup.ts,monitor-run-start.ts,plugin-config-update.ts, anddeco-sites.tsall call it with a deterministic, org-scoped id — so a double-submit or a racing webhook hits this today and a caller gets an opaque driver error instead of a message it can branch on.Failure scenario: two requests concurrently call
storage.connections.create({ id: "conn_x", ... })for the same deterministic id (e.g. two overlapping calls toreports/setup.tsfor the same org). Both pass the existence check, one insert wins, the other's insert throws a rawduplicate key value violates unique constraint "connections_pkey"error instead of"Connection ID already exists".Fix: wrap the insert in a try/catch and rethrow the same friendly error
createNew()already uses for this exact race — one code path, no behavior change on the non-racing path (same insert, same returned row).Regression test: added
apps/api/src/storage/connection.integration.test.ts— fires two concurrentcreate()calls for the same id and asserts any rejection carries the friendly message, not a raw driver error.Command a reviewer runs to confirm:
bun test apps/api/src/storage/connection.integration.test.ts(needs Postgres — same as every other test in that file; not runnable in this bot's sandbox).Locally verified:
bun run fmt,cd apps/api && bunx tsc --noEmit(clean),bunx oxlinton both changed files (0 warnings/errors). The integration test itself needs a real Postgres instance, which isn't available in this sandbox — full CI validates it.Summary by cubic
Surfaces a friendly "Connection ID already exists" error when concurrent
ConnectionStorage.create()calls race on the same id, instead of leaking the raw Postgres duplicate-key error. This matchescreateNew()and leaves non-racing behavior unchanged.Review notes
insertin try/catch; maps duplicate-key/unique-constraint errors to the friendly message, otherwise rethrows the original error.create()calls and asserts any rejection carries the friendly message.Written for commit c089212. Summary will update on new commits.