forge
Explore
Log in

Don't you dare go Hollow

Stop a walk-out stamp reverting a submission

Browse files at this commit

3c6e9b93472eb5ba7dfeafa28b150d8b023df56cAhmet Kaan GümüşParent: 992619b
  • fix
    • stop a walk-out stamp reverting a submission
Show full message
🐛 fix(exams): stop a walk-out stamp reverting a submission

`ExamAttempt::set_left` overwrote the whole attempt row from a
pre-read snapshot. The exam room's `stamp_left` reads the attempt
(in-progress), then writes `left_at` — so a `finish` landing in that
gap was reverted: the stale snapshot's `finished_at=None` clobbered
the fresh submission, silently un-submitting the exam and, with
`allow_rejoin` off, locking the student out of a room they left after
finishing. Reachable when a student hits REST finish as their last
exam-room socket closes.

Write only the field now (`UPDATE $id SET left_at`), never the whole
row, so a stamp can't carry a stale `finished_at` back over a
concurrent submission. Regression test simulates the
read→finish→stale-write interleaving deterministically.
src/domain/exam_attempt.rsmodified+75 -4
@@ -241,14 +241,29 @@ impl ExamAttempt {
241 241
242 /// Stamp (or clear) the walked-out marker. The exam room sets it when the242 /// Stamp (or clear) the walked-out marker. The exam room sets it when the
243 /// student's socket closes mid-attempt and clears it when they come back.243 /// student's socket closes mid-attempt and clears it when they come back.
244+ ///
245+ /// Writes *only* the `left_at` field (never the whole row): the room's
246+ /// teardown reads the attempt, sees it in progress, then stamps here — and
247+ /// a submission that lands in that gap must survive. A whole-row write from
248+ /// the pre-read snapshot would carry its blank `finished_at` back over the
249+ /// fresh submission, silently un-submitting the exam (and, with
250+ /// `allow_rejoin` off, locking the student out).
244 pub async fn set_left(251 pub async fn set_left(
245- mut self,252+ self,
246 left_at: Option<Timestamp>,253 left_at: Option<Timestamp>,
247 db: &Database,254 db: &Database,
248 ) -> Result<ExamAttempt, AppError> {255 ) -> Result<ExamAttempt, AppError> {
249- self.left_at = left_at;256+ let mut result = db
250- let updated: Option<ExamAttempt> = db.update(self.id.record()).content(self).await?;257+ .query("UPDATE $id SET left_at = $left RETURN AFTER")
251- updated.ok_or(AppError::NotFound)258+ .bind(("id", self.id.record()))
259+ .bind(("left", left_at))
260+ .await?
261+ .check()?;
262+ result
263+ .take::<Vec<ExamAttempt>>(0)?
264+ .into_iter()
265+ .next()
266+ .ok_or(AppError::NotFound)
252 }267 }
253 268
254 /// One sitting by id — the exam room re-reads its own attempt this way,269 /// One sitting by id — the exam room re-reads its own attempt this way,
@@ -438,4 +453,60 @@ mod tests {
438 assert!(!created);453 assert!(!created);
439 assert_eq!(resumed.get_seq(), 2);454 assert_eq!(resumed.get_seq(), 2);
440 }455 }
456+
457+ #[tokio::test]
458+ async fn stamping_left_never_reverts_a_submission() {
459+ let db = init_mem().await.unwrap();
460+ let (exam, _question) = open_exam_with_question(&db, 1).await;
461+ let user = student();
462+
463+ // The student is sitting the exam.
464+ let (attempt, _) = ExamAttempt::start(&exam, &user, &db).await.unwrap();
465+ assert_eq!(attempt.get_seq(), 1);
466+
467+ // The exam-room teardown reads the attempt while it is still in
468+ // progress (`stamp_left`'s read) — a snapshot with `finished_at` unset.
469+ let stale = ExamAttempt::read(attempt.get_id(), &db)
470+ .await
471+ .unwrap()
472+ .expect("attempt exists");
473+ assert!(stale.get_finished_at().is_none());
474+
475+ // Before the teardown writes, the student submits (a REST finish, or a
476+ // finish over another socket) — the submission lands.
477+ let finished = ExamAttempt::read(attempt.get_id(), &db)
478+ .await
479+ .unwrap()
480+ .expect("attempt exists")
481+ .finish(&db)
482+ .await
483+ .unwrap();
484+ assert!(finished.get_finished_at().is_some());
485+
486+ // Now the last socket's teardown stamps `left_at` from its stale
487+ // snapshot. Stamping the walk-out must touch only `left_at` — it must
488+ // not carry the snapshot's blank `finished_at` back over the fresh
489+ // submission, or the student's submitted exam silently reopens (and,
490+ // with `allow_rejoin` off, locks them out of a room they left after
491+ // finishing).
492+ stale.set_left(Some(Timestamp::now()), &db).await.unwrap();
493+
494+ let after = ExamAttempt::read(attempt.get_id(), &db)
495+ .await
496+ .unwrap()
497+ .expect("attempt still exists");
498+ assert!(
499+ after.get_finished_at().is_some(),
500+ "stamping left_at must not revert a submission that raced it"
501+ );
502+ assert!(
503+ after.get_left_at().is_some(),
504+ "the walk-out is still recorded"
505+ );
506+ assert_eq!(
507+ after.status(&exam, Timestamp::now()),
508+ AttemptStatus::Submitted,
509+ "the attempt stays submitted"
510+ );
511+ }
441 }512 }