forge
Explore
Log in

Don't you dare go Hollow

🤖 answer a contended thread delete 204, not 500

Browse files at this commit

c998c0d62b34e04e637a3781ed9045b925ae3effAhmet Kaan GümüşParent: 2351107
  • fix
    • 🤖 answer a contended thread delete 204, not 500
Show full message
fix: 🤖 answer a contended thread delete 204, not 500

`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.rsmodified+30 -13
@@ -289,20 +289,37 @@ impl ChatbotThread {
289 /// Delete the thread and every turn in it — one transaction, so a crash289 /// Delete the thread and every turn in it — one transaction, so a crash
290 /// can't orphan messages under a vanished thread. The owner's slot comes290 /// can't orphan messages under a vanished thread. The owner's slot comes
291 /// back in that same transaction, or the cap would ratchet shut.291 /// back in that same transaction, or the cap would ratchet shut.
292+ ///
293+ /// A turn, a rename and this delete all write the thread row (see
294+ /// [`touch_and_write`]), so the store aborting one of them is ordinary
295+ /// here — that collision is exactly what keeps a turn from outliving the
296+ /// thread — and a lost round is re-sent rather than reported as the `500`
297+ /// it used to be. Re-sending is sound: every statement is a `DELETE` or a
298+ /// field-scoped `UPDATE`, none of which can ever answer "already exists"
299+ /// (see [`transaction_with_retry`]). On the re-sent round the thread is
300+ /// already gone, so the `RETURN` is empty and the caller gets the `404`
301+ /// that is the truth.
292 pub async fn delete(self, db: &Database) -> Result<ChatbotThread, AppError> {302 pub async fn delete(self, db: &Database) -> Result<ChatbotThread, AppError> {
293- let mut result = db303+ let (mut result, mut errors) = transaction_with_retry(
294- .query(304+ db,
295- "BEGIN TRANSACTION;305+ "BEGIN TRANSACTION;
296- DELETE chatbot_message WHERE thread_id = $conv;306+ DELETE chatbot_message WHERE thread_id = $conv;
297- LET $gone = (DELETE $conv RETURN BEFORE);307+ LET $gone = (DELETE $conv RETURN BEFORE);
298- UPDATE $usr SET chatbot_thread_count = math::max([(chatbot_thread_count ?? 0) - array::len($gone), 0]);308+ UPDATE $usr SET chatbot_thread_count = math::max([(chatbot_thread_count ?? 0) - array::len($gone), 0]);
299- RETURN $gone;309+ RETURN $gone;
300- COMMIT TRANSACTION;",310+ COMMIT TRANSACTION;",
301- )311+ &[
302- .bind(("conv", self.id.record()))312+ ("conv".into(), self.id.record().into_value()),
303- .bind(("usr", self.user_id.record()))313+ ("usr".into(), self.user_id.record().into_value()),
304- .await?314+ ],
305- .check()?;315+ &[],
316+ )
317+ .await?;
318+ if let Some(error) = errors.drain().map(|(_, error)| error).next() {
319+ return Err(error.into());
320+ }
321+ // Slots count BEGIN, the cascade, the LET and the UPDATE: the RETURN
322+ // is slot 4.
306 let deleted: Option<ChatbotThread> =323 let deleted: Option<ChatbotThread> =
307 result.take::<Vec<ChatbotThread>>(4)?.into_iter().next();324 result.take::<Vec<ChatbotThread>>(4)?.into_iter().next();
308 deleted.ok_or(AppError::NotFound)325 deleted.ok_or(AppError::NotFound)