fix: send createSession 201 only after the transaction commits - #1461
fix: send createSession 201 only after the transaction commits#1461ionfwsrijan wants to merge 1 commit into
Conversation
The success response and 4xx validation returns were issued inside the withTransaction callback, so a 201 could reach the client before the commit finished (phantom success on rollback), and returning res inside the callback resolved it as a commit instead of an abort. Move validation ahead of the transaction, return the created session from the callback, and send the response only after withTransaction resolves. Closes Canopus-Labs#1442
|
Warning Review limit reached
Next review available in: 50 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
Comment |
Problem
createSession(backend/controllers/sessionController.js) runs all writes insidemongoSession.withTransaction(async () => { ... }), but:res.status(201).json(...)is sent inside the callback — before Mongoose commits. If the commit subsequently fails (network error, replica-set failover, duplicate key on a concurrent write), the client already got a201+ full session object while the transaction was rolled back: a phantom success.return res.status(400)inside the callback. Mongoose'swithTransactioncommits based on the callback's return value, so returning an Express response object commits instead of aborting — the opposite of the code's obvious intent.Fix
role/experience/MAX_SESSIONSchecks now run first and return 4xx from the outer handler, never from inside the callback.res.*out of the callback — the callback returns the created session, andres.status(201).json(...)is sent only afterwithTransactionresolves (i.e. after commit).SESSION_LIMIT_REACHEDthrow is replaced by an early 400 return ahead of the transaction.endSession()stays infinally, guarded for the pre-transaction validation paths.Files changed
backend/controllers/sessionController.js— restructuredcreateSession.backend/tests/sessionController.create.transaction.unit.test.js— tests for 400s without starting a transaction, no 201 on commit failure, and 201 only after commit.Testing
npx vitest run tests/sessionController.create.transaction.unit.test.js— 5/5 passing.Closes #1442