📝 stop a sitting from starting inside an exam delete
Browse files at this commit00207fe9e9a684b2895ada98a63ad937e4bd54fcParent: 226334a- fix
- 📝 stop a sitting from starting inside an exam delete
2 changed files +150 -0
- src/web/exams/mod.rs +23 -0
- tests/regress_exam_delete.rs +127 -0
src/web/exams/mod.rsmodified+23 -0
| @@ -588,6 +588,29 @@ async fn delete_exam( | |||
| 588 | "only the course creator, an assigned teacher, or a manager/admin can delete this exam", | 588 | "only the course creator, an assigned teacher, or a manager/admin can delete this exam", |
| 589 | )); | 589 | )); |
| 590 | } | 590 | } |
| 591 | + // *Writer* lease of [`EXAM_LOCK`] across the whole cascade, blob names | ||
| 592 | + // included — the lease `delete_homework` has always held, and its absence | ||
| 593 | + // here is what made a sitting able to start inside this delete. Every other | ||
| 594 | + // child of an exam now writes the exam row in its own transaction, so the | ||
| 595 | + // store refuses the pair; an attempt cannot, because its claim lands on the | ||
| 596 | + // *student's* row (`exam_sat_total`) and touches nothing this delete | ||
| 597 | + // writes. `start_attempt` already takes the writer lease from its exam read | ||
| 598 | + // through the insert, so this one lease is the whole ordering: a start | ||
| 599 | + // either finishes before the sweep (which then takes its row) or reads no | ||
| 600 | + // exam at all and is a 404. Left orphaned, that attempt kept a sitting on | ||
| 601 | + // the student's lifetime counter and could mint a badge — awards are | ||
| 602 | + // add-only and never revoked — for an exam that never existed. | ||
| 603 | + // | ||
| 604 | + // It spans the blob names too: they are collected *before* the rows go, so | ||
| 605 | + // an image row written after that snapshot would strand its bytes on disk | ||
| 606 | + // even though the row itself is now refused. | ||
| 607 | + // | ||
| 608 | + // ponytail: process-local, so it holds because the deployment is a single | ||
| 609 | + // replica with stop-the-world deploys (two overlapping binaries would | ||
| 610 | + // reopen it). Closing it in the store means the `cap` shape the counter | ||
| 611 | + // work already sketched: `claim_and_create` gaining a second record to | ||
| 612 | + // touch, so the attempt writes the exam key as every other child does. | ||
| 613 | + let _guard = EXAM_LOCK.write().await; | ||
| 591 | // Rows go first (the delete cascades them), blobs after — a crash in | 614 | // Rows go first (the delete cascades them), blobs after — a crash in |
| 592 | // between strands at worst an unreachable blob. | 615 | // between strands at worst an unreachable blob. |
| 593 | let images = QuestionImage::list_for_exam(exam.get_id(), &st.db).await?; | 616 | let images = QuestionImage::list_for_exam(exam.get_id(), &st.db).await?; |
tests/regress_exam_delete.rsadded+127 -0
| @@ -0,0 +1,127 @@ | |||
| 1 | +//! An exam delete must take every child with it — including the one child the | ||
| 2 | +//! store cannot refuse on its own. | ||
| 3 | +//! | ||
| 4 | +//! Every other child of an exam (answers, drawings, questions, pictures) writes | ||
| 5 | +//! the exam row inside its own transaction, so a delete racing one of them | ||
| 6 | +//! contends on a single key and the store aborts a side. An attempt cannot do | ||
| 7 | +//! that: its create rides `cap::claim_and_create` against the *student's* row | ||
| 8 | +//! (`exam_sat_total`), which this delete never touches, so nothing collides and | ||
| 9 | +//! the freeze/existence gate is a plain read. `start_attempt` has always held | ||
| 10 | +//! `EXAM_LOCK.write()` from its exam read through the insert; `delete_exam` | ||
| 11 | +//! held nothing at all, and that asymmetry is what let a sitting start inside | ||
| 12 | +//! the delete window and outlive the exam. | ||
| 13 | +//! | ||
| 14 | +//! This suite drives the real handlers, because the lease being tested lives in | ||
| 15 | +//! the web layer and no domain call can observe it. | ||
| 16 | + | ||
| 17 | +mod common; | ||
| 18 | + | ||
| 19 | +use axum::http::StatusCode; | ||
| 20 | +use common::{app_and_db, create_exam_with, enroll, id_of, login_as, me_id, send}; | ||
| 21 | +use hezarfen_backend::domain::exam::ExamId; | ||
| 22 | +use hezarfen_backend::domain::exam_attempt::ExamAttempt; | ||
| 23 | +use serde_json::json; | ||
| 24 | + | ||
| 25 | +/// The delete is held open for a second *after* the exam row is gone but | ||
| 26 | +/// before its cascade runs — a `DEFINE EVENT` on the table fires inside the | ||
| 27 | +/// delete's own transaction, so the window is opened by the database rather | ||
| 28 | +/// than by a lucky interleaving. A start firing into that window used to read | ||
| 29 | +/// an exam that was still there (deleted, uncommitted), create its sitting, and | ||
| 30 | +/// commit past a sweep that had already run on a snapshot without it. That row | ||
| 31 | +/// was unreachable afterwards — every route to an attempt goes through its exam | ||
| 32 | +/// — while the student's lifetime sitting counter stayed up for good, and a | ||
| 33 | +/// badge minted off it is never revoked. | ||
| 34 | +/// | ||
| 35 | +/// The in-memory engine is enough here, unusually: this asserts a *lock*, not | ||
| 36 | +/// the store's conflict detection, and a mutex behaves the same on either | ||
| 37 | +/// engine. Mutation-tested — dropping the lease from `delete_exam` turns it | ||
| 38 | +/// red. | ||
| 39 | +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] | ||
| 40 | +async fn an_attempt_started_inside_a_delete_never_outlives_the_exam() { | ||
| 41 | + let (app, db) = app_and_db().await; | ||
| 42 | + let teacher = login_as(&app, &db, "ogretmen_sil", "teacher").await; | ||
| 43 | + let student = login_as(&app, &db, "ogrenci_sil", "student").await; | ||
| 44 | + let student_id = me_id(&app, &student).await; | ||
| 45 | + | ||
| 46 | + // Hold the delete open for a full second once the row is gone, while its | ||
| 47 | + // cascade still has to run. | ||
| 48 | + db.query( | ||
| 49 | + "DEFINE EVENT hold_the_window ON TABLE exam WHEN $event = 'DELETE' \ | ||
| 50 | + THEN { SLEEP 1s; };", | ||
| 51 | + ) | ||
| 52 | + .await | ||
| 53 | + .expect("define the window event") | ||
| 54 | + .check() | ||
| 55 | + .expect("check the window event"); | ||
| 56 | + | ||
| 57 | + let mut sittings = 0; | ||
| 58 | + for round in 0..3 { | ||
| 59 | + let course = common::create_course(&app, &teacher, &format!("Fizik {round}")).await; | ||
| 60 | + enroll(&app, &teacher, &course, &student_id).await; | ||
| 61 | + // `open` mode, so the exam is actually sittable: an unscheduled one | ||
| 62 | + // answers the start with a 409 before it ever writes, which would make | ||
| 63 | + // this test pass on a missing lease. | ||
| 64 | + let res = create_exam_with( | ||
| 65 | + &app, | ||
| 66 | + &teacher, | ||
| 67 | + &course, | ||
| 68 | + json!({ "title": "quiz", "kind": "midterm", "mode": "open" }), | ||
| 69 | + ) | ||
| 70 | + .await; | ||
| 71 | + assert_eq!( | ||
| 72 | + res.status, | ||
| 73 | + StatusCode::CREATED, | ||
| 74 | + "round {round}: create exam" | ||
| 75 | + ); | ||
| 76 | + let exam = id_of(&res.body); | ||
| 77 | + | ||
| 78 | + let drop_it = { | ||
| 79 | + let (app, teacher, exam) = (app.clone(), teacher.clone(), exam.clone()); | ||
| 80 | + tokio::spawn(async move { | ||
| 81 | + send( | ||
| 82 | + &app, | ||
| 83 | + "DELETE", | ||
| 84 | + &format!("/exams/{exam}"), | ||
| 85 | + Some(&teacher), | ||
| 86 | + None, | ||
| 87 | + ) | ||
| 88 | + .await | ||
| 89 | + }) | ||
| 90 | + }; | ||
| 91 | + // The start fires inside the held window: without the lease it reads an | ||
| 92 | + // exam row the delete has removed but not committed. | ||
| 93 | + tokio::time::sleep(std::time::Duration::from_millis(300)).await; | ||
| 94 | + let sit = { | ||
| 95 | + let (app, student, exam) = (app.clone(), student.clone(), exam.clone()); | ||
| 96 | + tokio::spawn(async move { | ||
| 97 | + send( | ||
| 98 | + &app, | ||
| 99 | + "POST", | ||
| 100 | + &format!("/exams/{exam}/attempt"), | ||
| 101 | + Some(&student), | ||
| 102 | + None, | ||
| 103 | + ) | ||
| 104 | + .await | ||
| 105 | + }) | ||
| 106 | + }; | ||
| 107 | + let (dropped, sat) = (drop_it.await.unwrap(), sit.await.unwrap()); | ||
| 108 | + assert_eq!( | ||
| 109 | + dropped.status, | ||
| 110 | + StatusCode::NO_CONTENT, | ||
| 111 | + "round {round}: the delete must succeed" | ||
| 112 | + ); | ||
| 113 | + // A 404 for the start is the correct answer; the only defect is stored | ||
| 114 | + // state. Nothing may 500 either way. | ||
| 115 | + assert!( | ||
| 116 | + sat.status == StatusCode::NOT_FOUND || sat.status.is_success(), | ||
| 117 | + "round {round}: a raced start must be answered, not {}: {:?}", | ||
| 118 | + sat.status, | ||
| 119 | + sat.body | ||
| 120 | + ); | ||
| 121 | + | ||
| 122 | + // Stored state is the whole verdict; a response code is not evidence. | ||
| 123 | + let id = ExamId::from_key(&exam); | ||
| 124 | + sittings += ExamAttempt::list_for_exam(&id, &db).await.unwrap().len(); | ||
| 125 | + } | ||
| 126 | + assert_eq!(sittings, 0, "a sitting outlived its exam"); | ||
| 127 | +} | ||