Skip to content

fix(server): prevent send-on-closed-channel panic in session title generation#3832

Open
Piyush0049 wants to merge 1 commit into
docker:mainfrom
Piyush0049:fix/session-manager-concurrency-panic
Open

fix(server): prevent send-on-closed-channel panic in session title generation#3832
Piyush0049 wants to merge 1 commit into
docker:mainfrom
Piyush0049:fix/session-manager-concurrency-panic

Conversation

@Piyush0049

Copy link
Copy Markdown
Contributor

Description

This PR fixes a critical runtime concurrency bug in SessionManager.RunSession where asynchronous session title generation could attempt to send events on a closed channel, causing the server process to crash.

Root Cause

When a session turn required title generation, sm.generateTitle was spawned asynchronously as a background goroutine writing title events to streamChan. If the conversation stream (RunStream) terminated early due to cancellation, completion, or error, the function returned immediately and triggered defer close(streamChan). When the background title goroutine subsequently attempted to emit runtime.SessionTitle to streamChan, sending on the closed channel caused a runtime panic.

Key Changes

  • WaitGroup Lifecycle Coordination: Added a sync.WaitGroup around the asynchronous generateTitle goroutine and placed wg.Wait() before the return paths in RunSession. This guarantees that close(streamChan) cannot execute until title generation completes or safely aborts on context cancellation.
  • Graceful Stream Consumer Loop: Replaced a direct return with break inside the RunStream consumer loop when encountering a context error. This ensures execution always falls through to wg.Wait(), guaranteeing background synchronization even during early client disconnects or stream aborts.

Verification

  • Ran task lint and golangci-lint run cleanly with zero issues reported.
  • Verified that context cancellation in generateTitle unblocks immediately via select on ctx.Done(), preventing hangs or deadlocks.

@Piyush0049
Piyush0049 requested a review from a team as a code owner July 25, 2026 15:48
@aheritier aheritier added area/core Core agent runtime, session management kind/fix PR fixes a bug (maps to fix:). Use on PRs only. labels Jul 25, 2026
@Piyush0049
Piyush0049 force-pushed the fix/session-manager-concurrency-panic branch from 9065123 to 89df77d Compare July 25, 2026 18:29
@aheritier

Copy link
Copy Markdown
Contributor

Same here @Piyush0049

Please provide more details about the issue you are trying to solve. Is it something which happens often? What are the symptoms?

Right now the PR doesn't provide enough context to understand if you are fixing a bug or if these are just code reviews by an LLM which could easily hallucinate bugs.

Thanks

@aheritier
aheritier requested a review from docker-agent July 26, 2026 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Core agent runtime, session management kind/fix PR fixes a bug (maps to fix:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants