Repository navigation
fix(root): create new jobs atomically with RETURNING id - #724
aloneangel45 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
1 issue found across 2 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="tests/pages/root.test.ts">
<violation number="1" location="tests/pages/root.test.ts:156">
P2: These concurrency tests cannot catch the bug they are meant to guard. Every handler runs its insert and id lookup synchronously, so the old `INSERT` + `SELECT` code would also pass. To exercise the race, the test needs a real interleaving, for example separate processes or connections against the same database file. At minimum, confirm the tests fail on the pre-PR root.tsx before relying on them as regression coverage.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
| expect(jobsOf(user.id)).toHaveLength(1); | ||
| }); | ||
|
|
||
| test("assigns distinct job IDs to concurrent requests sharing unauthenticated user 0", async () => { |
There was a problem hiding this comment.
P2: These concurrency tests cannot catch the bug they are meant to guard. Every handler runs its insert and id lookup synchronously, so the old INSERT + SELECT code would also pass. To exercise the race, the test needs a real interleaving, for example separate processes or connections against the same database file. At minimum, confirm the tests fail on the pre-PR root.tsx before relying on them as regression coverage.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At tests/pages/root.test.ts, line 156:
<comment>These concurrency tests cannot catch the bug they are meant to guard. Every handler runs its insert and id lookup synchronously, so the old `INSERT` + `SELECT` code would also pass. To exercise the race, the test needs a real interleaving, for example separate processes or connections against the same database file. At minimum, confirm the tests fail on the pre-PR root.tsx before relying on them as regression coverage.</comment>
<file context>
@@ -152,4 +152,31 @@ describe("GET / without accounts (ALLOW_UNAUTHENTICATED)", () => {
expect(jobsOf(user.id)).toHaveLength(1);
});
+
+ test("assigns distinct job IDs to concurrent requests sharing unauthenticated user 0", async () => {
+ setEnv({ ALLOW_UNAUTHENTICATED: true, UNAUTHENTICATED_USER_SHARING: true });
+ const existingIds = new Set(jobsOf("0").map((job) => job.id));
</file context>
|
Addressed the @cubic-dev-ai test coverage finding in commit
|
@aloneangel45 Good — that addresses the finding. The trigger-based tests are the ones that actually pin the bug: in-process concurrent Two small things worth being aware of, neither blocking:
No further action needed on the original coverage finding. Tip: get faster answers by chatting with cubic’s review copilot in the review UI |
Fixes #720
Problem
In
src/pages/root.tsx, when a request arrived without ajobIdcookie, a new row was inserted intojobsand then fetched in a separate query:Under concurrent
GET /requests sharing the sameuser_id(such as anonymous user0whenUNAUTHENTICATED_USER_SHARING=true, or multiple simultaneous tabs for an authenticated user), both requests could insert rows and read back the same latestid.Fix
if (!jobId)and insert + return the newly created row ID atomically in a single statement usingINSERT INTO jobs (user_id, date_created) VALUES (?, ?) RETURNING id.tests/pages/root.test.tssending 10 concurrentGET /requests (for both shared anonymous user0and an authenticated user) and asserting 10 distinctjobIdcookies are issued.Verification
bun fmt --check src/pages/root.tsx tests/pages/root.test.ts— passedbun x eslint src/pages/root.tsx tests/pages/root.test.ts— passedbun run lint:tsc— passed (52files)bun test tests/pages/root.test.ts— 14 test cases passed (on Windows, only the sharedafterAllrmSynctemp cleanup intests/preload.tshitsEBUSYdue to the open module-level SQLite handle)Summary by cubic
Fixes #720 by making job creation atomic so concurrent requests no longer collide on the same job ID.
Previously,
GET /inserted a job row and then fetched the latest ID in a separate query, which let simultaneous requests for the same user read back the same ID. Now the insert returns the new ID directly viaRETURNING id, so each request gets its own job.Regression tests now cover both nondeterministic and deterministic races: 10 concurrent
GET /requests for the shared anonymous user and an authenticated user each receive distinct job IDs, and aTEMP TRIGGERthat forcibly interleaves another insert proves each request it keeps its own job ID.Written for commit 74034e0. Summary will update on new commits.