fix(questions): make addQuestionToSession atomic and bounded - #1472
fix(questions): make addQuestionToSession atomic and bounded#1472ionfwsrijan wants to merge 1 commit into
Conversation
|
Warning Review limit reached
Next review available in: 3 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 (3)
Comment |
| throw new Error("INVALID_INPUT"); | ||
| } | ||
|
|
||
| const session = await Session.findById(sessionId).session(mongoSession); |
| // this update commit or roll back together inside the transaction, so a | ||
| // failed session update can never leave orphaned questions. | ||
| await Session.updateOne( | ||
| { _id: sessionId }, |
|
Thank you for your contribution! To keep reviews manageable and maintain repository quality, contributors may have a maximum of 3 open Issues and 3 open Pull Requests at any given time. Please wait until one of your existing submissions is reviewed or closed before opening additional ones. If you believe this was closed by mistake, feel free to contact the maintainers. |
Problem
addQuestionToSessionperformed two writes that were not atomic:session.save()failed (or the connection dropped between thewrites), the
Questiondocuments were already inserted but never linked —permanently orphaned rows no API path surfaces.
addQuestionToSessioncalls each loaded the session,pushed their own IDs, and
save()d; the latersave()overwrote theearlier push, silently dropping one batch's linkage.
questionshad no upper bound per request.Fix
backend/controllers/questionController.js—addQuestionToSessionnowruns entirely inside a
mongoose.startSession()transaction (mirroringcreateSession/deleteSession), and the linkage is an atomic$push:Question.insertMany, and the session updatecommit or roll back together — a failed session update can no longer leave
orphaned questions.
Session.updateOne({ _id }, { $push: { questions: { $each: ids } } })instead of read-modify-write +
save(), so concurrent adds both persistwithout losing a batch.
backend/Input_validators/ValidateQuestions.js— thequestionsarray isnow capped at
max(50)per request.Files changed
backend/controllers/questionController.js- transactionaladdQuestionToSessionwith atomic$pushlinkage.backend/Input_validators/ValidateQuestions.js-questionscapped at 50.backend/tests/questionController.ownership.unit.test.js- rewrote with aModule._loadshim (the previousvi.mockcould not intercept the CJScontroller's
require()calls; it was already failing 5/5 onorigin/main).Testing
npx vitest run tests/questionController.ownership.unit.test.js- 6/6 pass:insertManyonce; linkage via$push: { questions: { $each } }(no
session.save()).withTransaction,so no orphaned questions are possible.
questionBank.unit.test.jsstill passes (6/6).Related
Closes #1453