Skip to content

fix(gateway): hold the agent lock when deleting a session - #103

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/high-severity-issues-d0f1
Draft

cursor[bot] wants to merge 1 commit into
mainfrom
cursor/high-severity-issues-d0f1

Conversation

@cursor

@cursor cursor Bot commented Sep 27, 2026

Copy link
Copy Markdown

Bug and impact

DELETE /api/sessions/:id runs on the gateway thread and called session_delete() with no agent mutex. agent_run() loads the transcript, spends the model call with that copy, then session_save() writes it back with INSERT ... ON CONFLICT DO UPDATE.

Trigger: open the dashboard, send a chat message, and delete that session while the reply is still in flight. The delete removes the row, then the in-flight save puts the old transcript back, including whatever the user was trying to remove.

Root cause

/reset already takes agent_lock() around session_delete() so it cannot race agent_run(). The dashboard route did not. The HTTP server is a separate thread from the main loop, and the race window is the whole model call.

Fix

dispatch_delete_session() takes the same mutex, deletes the row, and releases it. handle_session_delete uses that helper. The lock is not held across channel I/O.

Validation

make test_dispatch and ./build/test_dispatch (new case asserts the mutex is held during the delete and that the row is gone). src/gateway/routes.c compiles with -DSHELLCLAW_GATEWAY -Werror.

Open in Web聽View Automation聽

DELETE /api/sessions ran on the HTTP thread while agent_run still
held the loaded transcript. session_save then recreated the row, so
a dashboard delete during a reply restored the conversation.

dispatch_delete_session takes the same mutex /reset already uses.

Co-authored-by: esadrianno <esadrianno@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant