From 226334a84bd0abfbccdff4ab3681dca4f95d1931 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 5 Aug 2026 03:30:20 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=E2=9D=93=20stop=20a=20solution=20from?= =?UTF-8?q?=20outliving=20its=20pool=20question?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Offering a solution was a bare create guarded by a read of its question, and a question delete sweeps its solutions in its own transaction. An offer landing after that delete removed the row but before it committed read a question that was still there, while the sweep ran on a snapshot predating the offer — so both committed and neither caller was told: 4 of 4 raced rounds against a real server. The row it left is unreachable in every direction. Every route to a solution goes through its question, so no reader lists it, no author edits it, no delete clears it, and its photo blob stays on disk for good. The offer now moves the question's own asked_at and puts it straight back inside one transaction, so the two writes collide on one key and the store refuses a side. It goes through a helper on the question, the menu's shape, so the next child hung off a question inherits it. This row carries no counter or revision to bump and must not grow one for this alone; the value is restored by capture, so nothing outside the transaction ever observes the move. The delete rides the retry now — it contends with an offer by design, and a lost round through a bare check() was a 500 for a delete that only had to be re-sent. --- src/domain/pool_question.rs | 178 ++++++++++++++++++++++++++++++++++-- src/domain/solution.rs | 59 ++++++++++-- 2 files changed, 220 insertions(+), 17 deletions(-) diff --git a/src/domain/pool_question.rs b/src/domain/pool_question.rs index 39d31c2..8b63202 100644 --- a/src/domain/pool_question.rs +++ b/src/domain/pool_question.rs @@ -25,6 +25,72 @@ use crate::domain::user::UserId; use crate::error::{AppError, ValidationError}; use crate::validate::validate_required; +/// Move the question's `asked_at` **and** run `statement` — a write to +/// something hanging off a question — in one transaction, handing back the row +/// it returned. `NotFound` means the question is gone and nothing was written. +/// +/// The move is what makes the question's **existence** part of the child's own +/// write, the way [`crate::domain::menu::bump_menu_and_write`] does for a menu: +/// the `UPDATE` matches nothing once the row is deleted, and a delete racing +/// this one touches the very key this transaction writes, so the two cannot +/// both commit. Reading the question first does *not* survive that race — the +/// read sees a row [`PoolQuestion::delete`] has removed but not committed, +/// while its `DELETE solution WHERE question = $q` ran on a snapshot predating +/// this insert, so both commit and the child outlives its question with neither +/// caller told anything. +/// +/// `asked_at` rather than a revision column because this row carries no counter +/// to bump and must not grow one for this alone. It is put back *by captured +/// value* in the same transaction, so nothing outside ever observes the move +/// and the row is byte-identical afterwards (the pool lists order on it). +/// Writing the same value would buy nothing: an `UPDATE` that leaves the +/// document unchanged is elided and never reaches the store's write set, so it +/// collides with nothing. +/// +/// Admissible for [`transaction_with_retry`] as long as `statement` is: the +/// `UPDATE`s, `SELECT`, `IF`/`THROW` and `RETURN` can never answer "already +/// exists". +pub(crate) async fn bump_question_and_write( + question: &PoolQuestionId, + statement: &str, + mut bindings: Vec<(String, surrealdb::types::Value)>, + db: &Database, +) -> Result, AppError> { + bindings.push(("q".into(), question.record().into_value())); + let (mut result, mut errors) = transaction_with_retry( + db, + &format!( + "BEGIN TRANSACTION; + LET $was_asked = (SELECT VALUE asked_at FROM ONLY $q); + LET $bumped = (UPDATE $q SET asked_at = asked_at + 1 RETURN VALUE id); + IF array::len($bumped) = 0 {{ THROW 'no_question' }}; + UPDATE $q SET asked_at = $was_asked; + LET $row = ({statement}); + RETURN $row; + COMMIT TRANSACTION;" + ), + &bindings, + &["no_question"], + ) + .await?; + // An aborted transaction errors *every* slot, most with a generic "not + // executed" — only the THROW's own slot names the reason. + if errors + .values() + .any(|error| error.to_string().contains("no_question")) + { + return Err(AppError::NotFound); + } + if let Some(error) = errors.drain().map(|(_, error)| error).next() { + return Err(error.into()); + } + // The trailing `RETURN` is the last statement before `COMMIT`, so its slot + // follows the statement count rather than a hand-kept number; + // `num_statements` counts BEGIN and COMMIT. + let slot = result.num_statements().saturating_sub(2); + Ok(result.take::>(slot)?.into_iter().next()) +} + #[derive(Debug, Clone, PartialEq, Eq, SurrealValue)] pub struct PoolQuestionId(RecordId); @@ -324,20 +390,28 @@ impl PoolQuestion { /// the removed rows — the question's *and* the swept solutions' — so the /// caller can take every image blob off disk (solutions carry photos too, /// and a row-only sweep would strand theirs forever). + /// + /// Rides the retry because a solution offer now writes this very row + /// ([`bump_question_and_write`]): the two contend by design, and a lost + /// round through a bare `check()` was a 500 for a delete that only had to + /// be re-sent. Admissible — a `DELETE` can never answer "already exists". pub async fn delete( id: &PoolQuestionId, db: &Database, ) -> Result)>, AppError> { - let mut result = db - .query( - "BEGIN TRANSACTION; + let (mut result, mut errors) = transaction_with_retry( + db, + "BEGIN TRANSACTION; DELETE solution WHERE question = $q RETURN BEFORE; DELETE $q RETURN BEFORE; COMMIT TRANSACTION;", - ) - .bind(("q", id.record())) - .await? - .check()?; + &[("q".into(), id.record().into_value())], + &[], + ) + .await?; + if let Some(error) = errors.drain().map(|(_, error)| error).next() { + return Err(error.into()); + } // Slots count BEGIN: the solution sweep is slot 1, the question slot 2. let solutions = result.take::>(1)?; Ok(result @@ -642,4 +716,94 @@ mod tests { .is_none() ); } + + /// The [`crate::domain::exam_answer::ExamAnswer::save`] defect, one domain + /// over: a solution offered inside its question's delete window must not + /// outlive it. Guarding the offer by *reading* the question does not do it + /// — the read sees a row [`PoolQuestion::delete`] has removed but not + /// committed, while its `DELETE solution WHERE question = $q` ran on a + /// snapshot predating the offer, so both commit and the solution is left + /// pointing at a question that is gone. Nothing can reach it after that: + /// every route to a solution goes through its question, so neither a + /// reader nor a second delete can ever clear it, and its photo stays on + /// disk for good. [`bump_question_and_write`] moves the question's own + /// `asked_at` instead, so the two transactions touch one key and the store + /// refuses one of them. + /// + /// The window is opened by the database, not by a lucky interleaving: a + /// `DEFINE EVENT` on `pool_question` fires *inside* the delete's own + /// transaction the instant the row goes, so the `SLEEP` lands exactly + /// between the delete and its cascade every time. + /// + /// Real server, and `#[ignore]`d for it: the subject *is* the store's + /// conflict detection, which `init_mem`'s embedded engine does not have — + /// it commits both writes and answers `Ok` to each, so this passes there on + /// broken code. + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] + #[ignore = "needs a real SurrealDB server: podman start hezarfen-surrealdb && cargo test -- --ignored"] + async fn a_solution_offered_inside_a_delete_never_outlives_its_question() { + let (db, _serialized) = database::init_test_server("solution_race").await; + // Hold the delete open for a full second after the row is gone, while + // its cascade still has to run. + db.query( + "DEFINE EVENT hold_the_window ON TABLE pool_question WHEN $event = 'DELETE' \ + THEN { SLEEP 1s; };", + ) + .await + .unwrap() + .check() + .unwrap(); + + let (mut solutions, mut swept, mut delete_500) = (0, 0, 0); + for round in 0..4 { + let asker = UserId::from_key(&Ulid::new().to_string()); + let helper = UserId::from_key(&Ulid::new().to_string()); + let q = question(&asker).insert(&db).await.unwrap(); + let id = q.get_id().clone(); + + let drop_it = { + let (db, id) = (db.clone(), id.clone()); + tokio::spawn(async move { PoolQuestion::delete(&id, &db).await }) + }; + // The offer starts inside the held window — the question row is + // gone but uncommitted, which is exactly what a read believes. + tokio::time::sleep(std::time::Duration::from_millis(300)).await; + let child = { + let (db, id) = (db.clone(), id.clone()); + tokio::spawn(async move { + Solution::new( + &id, + &helper, + SolutionBody::try_new("Kismi integrasyon uygula.").unwrap(), + ) + .insert(&db) + .await + }) + }; + let (drop_it, child) = (drop_it.await.unwrap(), child.await.unwrap()); + // A 404 for the offer is a correct answer; the only defect is + // stored state. The delete must not 500 either — it contends with + // the offer by design and a lost round is re-sent, not reported. + assert!( + !matches!(child, Err(AppError::Db(_))), + "round {round}: a raced offer must be answered, not 500: {child:?}" + ); + if matches!(drop_it, Err(AppError::Db(_))) { + delete_500 += 1; + } + + // Stored state is the whole verdict; a return value is not evidence. + if PoolQuestion::read(&id, &db).await.unwrap().is_none() { + swept += 1; + solutions += Solution::list_for(&id, None, 0, &db).await.unwrap().0.len(); + } + } + eprintln!("PoolQuestion::delete raced by an offer: {swept}/4 rounds deleted the question"); + assert!( + swept > 0, + "no round ever deleted the question, so the window was never reached" + ); + assert_eq!(solutions, 0, "a solution outlived its question"); + assert_eq!(delete_500, 0, "a raced delete must retry, not 500"); + } } diff --git a/src/domain/solution.rs b/src/domain/solution.rs index e47afc9..8d5d7e8 100644 --- a/src/domain/solution.rs +++ b/src/domain/solution.rs @@ -16,7 +16,7 @@ use crate::database::Database; use crate::domain::monotonic_id::next_ulid; use crate::domain::note_file::FileContentType; use crate::domain::page::PagedList; -use crate::domain::pool_question::PoolQuestionId; +use crate::domain::pool_question::{PoolQuestionId, bump_question_and_write}; use crate::domain::timestamp::Timestamp; use crate::domain::user::UserId; use crate::error::{AppError, ValidationError}; @@ -130,10 +130,32 @@ impl Solution { self.image_size } + /// Offer the solution. `NotFound` = the question is gone, and nothing was + /// written: the create rides [`bump_question_and_write`], so the question's + /// existence is a *write* to its row rather than a read the handler made a + /// moment earlier — a bare create landing inside + /// [`PoolQuestion::delete`](crate::domain::pool_question::PoolQuestion::delete)'s + /// window was swept by nothing and left a solution no route could ever + /// reach or remove (every path to one goes through its question), photo + /// blob included. + /// + /// Re-sendable despite the `CREATE`: `$id` is a ULID minted once per call + /// on a table with no `UNIQUE` index, so a re-send cannot answer "already + /// exists" — the one thing the retry could not survive. pub async fn insert(self, db: &Database) -> Result { // whole-row-save-ok: create of a fresh ULID row built in place by `new` — there is no prior row to clobber - let created: Option = db.create(self.id.record()).content(self).await?; - created.ok_or_else(|| AppError::Internal("failed to create solution".into())) + let (question, id) = (self.question.clone(), self.id.record()); + bump_question_and_write( + &question, + "CREATE $id CONTENT $solution", + vec![ + ("id".into(), id.into_value()), + ("solution".into(), self.into_value()), + ], + db, + ) + .await? + .ok_or_else(|| AppError::Internal("failed to create solution".into())) } /// Read a solution only if it belongs to `question` — keeps the nested @@ -256,11 +278,28 @@ mod tests { use super::*; + /// A real question row: offering a solution moves its question's + /// `asked_at` (that is what keeps a solution from outliving its question), + /// so a minted id nothing wrote is a 404. + async fn question_row(db: &Database) -> PoolQuestionId { + use crate::domain::pool_question::{PoolQuestion, PoolQuestionBody, PoolQuestionTitle}; + PoolQuestion::new( + &UserId::from_key(&Ulid::new().to_string()), + PoolQuestionTitle::try_new("soru").unwrap(), + PoolQuestionBody::try_new("neden").unwrap(), + ) + .insert(db) + .await + .unwrap() + .get_id() + .clone() + } + #[tokio::test] async fn rows_scope_to_their_question_and_list_oldest_first() { let db = crate::database::init_mem().await.unwrap(); - let question_a = PoolQuestionId::generate(); - let question_b = PoolQuestionId::generate(); + let question_a = question_row(&db).await; + let question_b = question_row(&db).await; let author = UserId::from_key(&Ulid::new().to_string()); // Distinct offer times so the assertion pins the real contract — @@ -322,7 +361,7 @@ mod tests { #[tokio::test] async fn image_set_replace_clear_report_the_replaced_blob() { let db = crate::database::init_mem().await.unwrap(); - let question = PoolQuestionId::generate(); + let question = question_row(&db).await; let author = UserId::from_key(&Ulid::new().to_string()); let png = FileContentType::try_new("image/png").unwrap(); @@ -388,7 +427,7 @@ mod tests { #[tokio::test] async fn set_body_edits_in_place() { let db = crate::database::init_mem().await.unwrap(); - let question = PoolQuestionId::generate(); + let question = question_row(&db).await; let author = UserId::from_key(&Ulid::new().to_string()); let solution = Solution::new( @@ -427,9 +466,9 @@ mod tests { #[tokio::test] async fn counts_for_groups_per_question() { let db = crate::database::init_mem().await.unwrap(); - let two = PoolQuestionId::generate(); - let one = PoolQuestionId::generate(); - let none = PoolQuestionId::generate(); + let two = question_row(&db).await; + let one = question_row(&db).await; + let none = question_row(&db).await; let author = UserId::from_key(&Ulid::new().to_string()); for (question, bodies) in [(&two, vec!["a", "b"]), (&one, vec!["c"])] {