From c47fb00c6eb3f78a54bb85101a44d2dc980d14f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 1 Aug 2026 04:12:49 +0300 Subject: [PATCH] =?UTF-8?q?docs:=20=F0=9F=93=90=20tell=20the=20truth=20abo?= =?UTF-8?q?ut=20a=20board=20body?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rejecting unknown keys made POST and PATCH /boards answer 422, a status neither utoipa block declared, so a generated client had no case for the first status a read-modify-write caller hits. Both now list it. The README went further and promised the opposite of what ships: it said the shared `participants` spelling meant a client "may post back a board it just read", but a board response carries id, creator, locked_by and epoch — all unknown to the request — so a verbatim round-trip is exactly the 422. It now says to send the writable fields only. The race test that comes with this is a smoke test and says so: cutting the retry to a single attempt leaves it green in five runs of five, while the same cut reddens its course.rs twin in two of three. Nothing exercises this delete's retry; it stays because the batch is admissible for it, not because a test has caught it losing a round. --- README.md | 13 ++++-- src/domain/board.rs | 104 ++++++++++++++++++++++++++++++++++++++++++++ src/domain/cap.rs | 5 ++- src/web/boards.rs | 2 + 4 files changed, 119 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d33cacc..688a028 100644 --- a/README.md +++ b/README.md @@ -2510,10 +2510,15 @@ model — no course, no lesson session, no appointment. Any account from alone clears, locks, closes or deletes.** The roster is spelled `participants` everywhere — in the `POST` body, in the -`PATCH` body and in every board response — so a client may post back a board it -just read. Unlike the rest of this API, the two board request bodies **reject -an unknown key** (`422`) rather than ignoring it: a misspelled roster would -otherwise open a board its author is silently alone on. +`PATCH` body and in every board response — so the field never changes name +between a request and a reply. That is the *only* thing the shared spelling +buys: unlike the rest of this API, the two board request bodies **reject an +unknown key** (`422`) rather than ignoring it, because a misspelled roster +would otherwise open a board its author is silently alone on. So a body must +carry **only** the fields that request accepts — a board it just read is not a +legal body, since `id`, `creator`, `locked_by`, `epoch` and the rest are all +unknown to `POST` and `PATCH`. A read-modify-write client sends the writable +fields it changed, never the whole board object back. **No parents, anywhere.** The `parent` role is the school's read-only observer, and it has no whiteboard access at all — not a view-only tier, none. It is diff --git a/src/domain/board.rs b/src/domain/board.rs index 514af70..770f937 100644 --- a/src/domain/board.rs +++ b/src/domain/board.rs @@ -498,4 +498,108 @@ mod tests { .unwrap(); assert_eq!(result.take::>>(0).unwrap()[0], vec![7, 9]); } + + /// [`Board::delete`] cascades the stroke log and decrements the creator's + /// `board_count` off the very record a concurrent stroke claim increments, + /// so the two contend by design. Losing that round writes nothing, which is + /// what makes re-sending it the recovery; without + /// [`crate::database::transaction_with_retry`] a lost round comes out as a + /// 500. + /// + /// A refusal or an `Err(NotFound)` is *correct* here — the board really is + /// gone — and must not fail this test. The only defect is `AppError::Db`. + /// + /// Multi-threaded and on a real server for the reason spelled out on + /// [`super::super::course`]'s twin: the current-thread runtime never + /// interleaves the two, and the embedded engine does not conflict-check + /// concurrent writes to one record at all. + /// + /// Mutation status, stated honestly: cutting + /// [`crate::database::transaction_with_retry`] to a single attempt leaves + /// this GREEN — 5 runs of 5, every one at 20/20 contended. The mutation is + /// real, not a dud: the same cut reddens the `course.rs` twin in 2 runs of + /// 3. So the delete here never actually loses a round in this window, and + /// nothing in the suite exercises its retry. Read this as a smoke test that + /// a contended delete does not 500 — the retry stays because the batch is + /// admissible for it and a lost round is possible in principle, not because + /// a test has ever caught it losing one. + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] + #[ignore = "needs a real SurrealDB server: podman start hezarfen-surrealdb && cargo test -- --ignored"] + async fn a_delete_racing_a_stroke_never_answers_500() { + use crate::domain::board_stroke::BoardStroke; + let (db, _serialized) = crate::database::init_test_server("board_delete_race").await; + db.query("CREATE user:c SET username = 'c', password_hash = 'x';") + .await + .unwrap() + .check() + .unwrap(); + let (mut delete_500, mut stroke_500, mut drawn) = (0, 0, 0); + let (mut last_delete, mut last_stroke) = (String::new(), String::new()); + for round in 0..20 { + let board = a_board(&db).await; + // The delete is held back by a sweeping beat: released together it + // is one statement while an append spends a read before it claims, + // so it would win every round and the guard would never be + // contended at all. Six racers over a 0-3ms sweep put the delete + // somewhere inside the counter writes instead. + let drop_it = { + let (board, db) = (board.clone(), db.clone()); + let beat = std::time::Duration::from_millis(round % 4); + tokio::spawn(async move { + tokio::time::sleep(beat).await; + board.delete(&db).await + }) + }; + let marks: Vec<_> = (0..6) + .map(|mark| { + let (id, db) = (board.get_id().clone(), db.clone()); + let author = user("c"); + tokio::spawn(async move { + BoardStroke::append(&id, &author, &format!("{{\"m\":{mark}}}"), 0, &db) + .await + }) + }) + .collect(); + let drop_it = drop_it.await.unwrap(); + if matches!(drop_it, Err(AppError::Db(_))) { + delete_500 += 1; + last_delete = format!("{drop_it:?}"); + } + // Contention is counted off the claim's own outcome, not off stored + // rows: the delete cascades `board_stroke`, so a stroke that landed + // and then lost its board leaves nothing behind to count. An `Ok` + // means the claim committed against the board row the delete was + // tearing down, which is exactly the overlap being measured. + let mut claimed = 0; + for mark in marks { + let mark = mark.await.unwrap(); + if matches!(mark, Err(AppError::Db(_))) { + stroke_500 += 1; + last_stroke = format!("{mark:?}"); + } + if mark.is_ok() { + claimed += 1; + } + } + if claimed > 0 { + drawn += 1; + } + } + eprintln!( + "Board::delete raced: {delete_500}/20 delete 500s, {stroke_500} stroke 500s, \ + {drawn}/20 rounds with a stroke claimed" + ); + assert!( + drawn > 0, + "no round ever landed a stroke, so the delete's cascade was never contended" + ); + assert_eq!( + delete_500, 0, + "a raced delete must retry, not 500: {delete_500}/20 rounds, last {last_delete}" + ); + assert_eq!( + stroke_500, 0, + "a raced stroke must retry, not 500: {stroke_500}/20 rounds, last {last_stroke}" + ); + } } diff --git a/src/domain/cap.rs b/src/domain/cap.rs index aef7284..4fc431b 100644 --- a/src/domain/cap.rs +++ b/src/domain/cap.rs @@ -272,7 +272,10 @@ pub(crate) enum ClaimedTwo { /// slots are the same type — and misreport that forever. The names are the /// guard against it. /// -/// The fields and `guard` are always in-crate constants, never user input. +/// The fields are always in-crate constants, and `guard` is either one or +/// built in-crate out of numbers this crate read back itself +/// (`board_stroke::append` appends an `epoch` it took off the board row) — +/// never text from a client. pub(crate) async fn claim_two_when_and_create( parent: &RecordId, soft_field: &str, diff --git a/src/web/boards.rs b/src/web/boards.rs index 302d73c..8b33c21 100644 --- a/src/web/boards.rs +++ b/src/web/boards.rs @@ -239,6 +239,7 @@ struct CreateBoard { (status = 401, description = "Not authenticated", body = ErrorResponse), (status = 403, description = "The caller is a parent", body = ErrorResponse), (status = 409, description = "The caller already holds the maximum number of boards", body = ErrorResponse), + (status = 422, description = "The body carries a key this request does not accept — a board response cannot be posted back verbatim"), ), )] async fn create_board( @@ -453,6 +454,7 @@ struct UpdateBoard { (status = 401, description = "Not authenticated", body = ErrorResponse), (status = 403, description = "Only the creator may change the participants or the lock", body = ErrorResponse), (status = 404, description = "Not found, or the caller is not on it", body = ErrorResponse), + (status = 422, description = "The body carries a key this request does not accept — a board response cannot be posted back verbatim"), ), )] async fn update_board(