From 5d69e6e21c31f76bc42be041d1d40f776c1d9ecb Mon Sep 17 00:00:00 2001 From: Hyeonsang Cho Date: Wed, 18 Nov 2026 22:22:01 +0900 Subject: [PATCH] fix(threads): answer 404 when a thread is deleted during a move (#8318) POST /api/threads/{id}/move takes no thread lock, so a DELETE can remove the row between set_project and the read-back. The route then built its response from None and failed with an unhandled 500. Return the route's existing 404 instead: the thread no longer exists. --- backend/app/gateway/routers/threads.py | 4 ++++ backend/tests/test_threads_router.py | 29 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/backend/app/gateway/routers/threads.py b/backend/app/gateway/routers/threads.py index d2ff57cc4d..bab50c4c1d 100644 --- a/backend/app/gateway/routers/threads.py +++ b/backend/app/gateway/routers/threads.py @@ -1481,6 +1481,10 @@ async def move_thread(thread_id: ThreadId, body: ThreadMoveRequest, request: Req if not moved: raise HTTPException(status_code=404, detail="Thread or project not found") record = await thread_store.get(thread_id) + if record is None: + # The move takes no thread lock, so a DELETE can remove the row + # between ``set_project`` and this read. The thread is gone either way. + raise HTTPException(status_code=404, detail="Thread or project not found") return ThreadResponse( thread_id=thread_id, status=record.get("status", "idle"), diff --git a/backend/tests/test_threads_router.py b/backend/tests/test_threads_router.py index c0dcf9d532..85f329eead 100644 --- a/backend/tests/test_threads_router.py +++ b/backend/tests/test_threads_router.py @@ -5549,6 +5549,35 @@ def test_move_thread_does_not_bump_updated_at(tmp_path): assert after == before +def test_move_thread_deleted_after_the_move_is_404(tmp_path): + """A DELETE that lands between the move and its re-read is a 404, not a 500. + + The move takes no thread lock, so a concurrent DELETE can remove the row + after ``set_project`` commits and before the route reads the record back. + """ + app = _build_project_threads_app(tmp_path) + project = _create_project(app) + store = app.state.thread_store + set_project = store.set_project + moves: list[bool] = [] + + async def set_project_then_concurrent_delete(thread_id, project_id, **kwargs): + moved = await set_project(thread_id, project_id, **kwargs) + moves.append(moved) + await store.delete(thread_id) + return moved + + store.set_project = set_project_then_concurrent_delete + with TestClient(app) as client: + thread_id = client.post("/api/threads", json={}).json()["thread_id"] + + response = client.post(f"/api/threads/{thread_id}/move", json={"project_id": project["id"]}) + + assert moves == [True] + assert response.status_code == 404, response.text + assert response.json()["detail"] == "Thread or project not found" + + def _seed_branchable_thread(app: FastAPI, thread_id: str) -> None: """Write a three-turn conversation so a middle AI turn can be branched.""" human_1 = HumanMessage(id="human-1", content="First question")