Skip to content

fix(server): one handle per session when two requests load it at once - #503

Merged
mrnasil merged 1 commit into
mainfrom
fix/server-session-load-race
Nov 25, 2026
Merged

mrnasil merged 1 commit into
mainfrom
fix/server-session-load-race

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 25, 2026

Copy link
Copy Markdown
Member

load_or_get_session checked the in-memory map, loaded the session from the database, then inserted it, and these steps were not atomic. After idle eviction, two requests for the same id (a run and a history poll, say) could both miss the map, each load its own copy, and the second insert replaced the first. A turn running on the replaced copy then saved over the other request's messages.

The new AppState::adopt_session inserts the loaded session only if the id is still absent, under the map lock, and otherwise returns the existing handle. Every request now shares one handle per session.

Test: loading the same id twice returns the same Arc, and it is the one stored in the map. The poisoned-map test also covers adopt_session. All 94 server tests pass, and lib.rs and routes.rs have 100% line coverage.

🤖 Generated with Claude Code

load_or_get_session checked the map, loaded from the database, then
inserted. After idle eviction, two requests for the same id (a run and
a history poll) could both miss, each load a copy, and the second
insert replaced the first. A turn on the replaced copy then saved over
the other's messages. The loaded session is now inserted only when the
id is still absent; otherwise the existing handle is returned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil enabled auto-merge (squash) November 25, 2026 15:55
@mrnasil
mrnasil merged commit b0967ce into main Nov 25, 2026
7 checks passed
@mrnasil
mrnasil deleted the fix/server-session-load-race branch November 25, 2026 16:42
@mrnasil mrnasil mentioned this pull request Nov 26, 2026
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