Skip to content

fix(session): prevent race condition in session cleanup#2503

Merged
dgageot merged 1 commit intodocker:mainfrom
cogvel:fix/session-close-race
Apr 24, 2026
Merged

fix(session): prevent race condition in session cleanup#2503
dgageot merged 1 commit intodocker:mainfrom
cogvel:fix/session-close-race

Conversation

@tdabasinskas
Copy link
Copy Markdown
Contributor

SessionManager.RunSession defers streaming.Unlock first and close(streamChan) last inside the background goroutine. Because defers run LIFO, close fires before Unlock, so a caller that drains the channel and immediately calls RunSession again can race the unlock and spuriously see ErrSessionBusy.

Reorder the defers so Unlock fires before close, making the channel-close the observable "session is idle" signal. Surfaced intermittently by TestRunSession_ConcurrentRequestReturnsErrSessionBusy in CI.

Reorder deferred cleanup operations to ensure the streaming lock is released before the channel closes. This prevents a race where a caller draining the stream channel and immediately starting a new session could see `ErrSessionBusy` because the lock hadn't been released yet.

Defers execute in LIFO order, so placing `streaming.Unlock` before `close(streamChan)` guarantees the lock is freed by the time the consumer's range loop terminates.
@tdabasinskas tdabasinskas requested a review from a team as a code owner April 24, 2026 09:28
@dgageot dgageot merged commit e12cc9a into docker:main Apr 24, 2026
9 checks passed
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.

2 participants