fix(server): prevent send-on-closed-channel panic in session title generation#3832
Open
Piyush0049 wants to merge 1 commit into
Open
fix(server): prevent send-on-closed-channel panic in session title generation#3832Piyush0049 wants to merge 1 commit into
Piyush0049 wants to merge 1 commit into
Conversation
Piyush0049
force-pushed
the
fix/session-manager-concurrency-panic
branch
from
July 25, 2026 18:29
9065123 to
89df77d
Compare
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a critical runtime concurrency bug in
SessionManager.RunSessionwhere 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.generateTitlewas spawned asynchronously as a background goroutine writing title events tostreamChan. If the conversation stream (RunStream) terminated early due to cancellation, completion, or error, the function returned immediately and triggereddefer close(streamChan). When the background title goroutine subsequently attempted to emitruntime.SessionTitletostreamChan, sending on the closed channel caused a runtime panic.Key Changes
sync.WaitGrouparound the asynchronousgenerateTitlegoroutine and placedwg.Wait()before the return paths inRunSession. This guarantees thatclose(streamChan)cannot execute until title generation completes or safely aborts on context cancellation.returnwithbreakinside theRunStreamconsumer loop when encountering a context error. This ensures execution always falls through towg.Wait(), guaranteeing background synchronization even during early client disconnects or stream aborts.Verification
task lintandgolangci-lint runcleanly with zero issues reported.generateTitleunblocks immediately viaselectonctx.Done(), preventing hangs or deadlocks.