From c998c0d62b34e04e637a3781ed9045b925ae3eff 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:23:28 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=A4=96=20answer=20a=20contended=20?= =?UTF-8?q?thread=20delete=20204,=20not=20500?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `ChatbotThread::delete` was the one delete in this domain sent as a bare guarded transaction with no retry loop, the shape `Board::delete` documents. It writes the thread row, and so does every turn, every rename and every other thread's cap claim on the same user — a lost round therefore surfaced as `AppError::Db`, a 500 for a delete that merely raced and would have succeeded on a re-send. Now that a turn deliberately collides with this batch, that round is no longer rare. Admissible by the stated rule: the batch is DELETE / DELETE / UPDATE only, none of which can legitimately answer "already exists". On a re-sent round the thread is already gone, the `RETURN` is empty, and the caller gets the 404 that is the truth rather than a 500. --- src/domain/chatbot_thread.rs | 43 +++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/src/domain/chatbot_thread.rs b/src/domain/chatbot_thread.rs index eaa3591..bd2552b 100644 --- a/src/domain/chatbot_thread.rs +++ b/src/domain/chatbot_thread.rs @@ -289,20 +289,37 @@ impl ChatbotThread { /// Delete the thread and every turn in it — one transaction, so a crash /// can't orphan messages under a vanished thread. The owner's slot comes /// back in that same transaction, or the cap would ratchet shut. + /// + /// A turn, a rename and this delete all write the thread row (see + /// [`touch_and_write`]), so the store aborting one of them is ordinary + /// here — that collision is exactly what keeps a turn from outliving the + /// thread — and a lost round is re-sent rather than reported as the `500` + /// it used to be. Re-sending is sound: every statement is a `DELETE` or a + /// field-scoped `UPDATE`, none of which can ever answer "already exists" + /// (see [`transaction_with_retry`]). On the re-sent round the thread is + /// already gone, so the `RETURN` is empty and the caller gets the `404` + /// that is the truth. pub async fn delete(self, db: &Database) -> Result { - let mut result = db - .query( - "BEGIN TRANSACTION; - DELETE chatbot_message WHERE thread_id = $conv; - LET $gone = (DELETE $conv RETURN BEFORE); - UPDATE $usr SET chatbot_thread_count = math::max([(chatbot_thread_count ?? 0) - array::len($gone), 0]); - RETURN $gone; - COMMIT TRANSACTION;", - ) - .bind(("conv", self.id.record())) - .bind(("usr", self.user_id.record())) - .await? - .check()?; + let (mut result, mut errors) = transaction_with_retry( + db, + "BEGIN TRANSACTION; + DELETE chatbot_message WHERE thread_id = $conv; + LET $gone = (DELETE $conv RETURN BEFORE); + UPDATE $usr SET chatbot_thread_count = math::max([(chatbot_thread_count ?? 0) - array::len($gone), 0]); + RETURN $gone; + COMMIT TRANSACTION;", + &[ + ("conv".into(), self.id.record().into_value()), + ("usr".into(), self.user_id.record().into_value()), + ], + &[], + ) + .await?; + if let Some(error) = errors.drain().map(|(_, error)| error).next() { + return Err(error.into()); + } + // Slots count BEGIN, the cascade, the LET and the UPDATE: the RETURN + // is slot 4. let deleted: Option = result.take::>(4)?.into_iter().next(); deleted.ok_or(AppError::NotFound)