fix(go): close failed session event loops - #2360
Merged
SteveSandersonMS merged 4 commits intoSep 3, 2026
Merged
Conversation
green3sf
marked this pull request as ready for review
August 19, 2026 12:43
SteveSandersonMS
force-pushed
the
agent/close-failed-session-event-loop
branch
from
September 3, 2026 12:37
868dfc3 to
014be61
Compare
ResumeSessionWithOptions pre-registers a session and starts its event loop before the RPC completes, the same eager-registration pattern CreateSession has. Every RPC-failure path called restoreReplacedSession (which correctly restores any session previously registered under this id, or deletes the map entry) but never closed the new, failed session's eventCh, leaking its processEvents goroutine per failed call - the same bug class this PR already fixes for CreateSession. Adds unit tests mirroring the existing CreateSession coverage. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Real-CLI, no-mocks coverage (goroutine counting across 20 repeated failures) for both CreateSession and ResumeSession failure paths. Verified this test fails against the pre-fix code (goroutine count grows by exactly one per failed call) and passes with the fix applied. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
CopilotSession has the identical eager-background-consumer pattern as Go: CreateSessionAsync/ResumeSessionAsync register the session and call StartProcessingEvents() (which starts ProcessEventsAsync() as a background task) before the RPC completes, but their catch blocks only called RemoveFromClient() - never completing _eventChannel - so the consumer task (and the object graph it implicitly keeps alive) runs forever on every failed call, expressed as a task/GC leak rather than a goroutine leak. Adds CopilotSession.CloseEventChannel()/Unregister() and wires both catch blocks (and DisposeAsync) through them. Adds a real-CLI E2E test (401 via proxy redirect for CreateSessionAsync, nonexistent-session-id for ResumeSessionAsync) using reflection to observe the session dictionary and event-channel completion state mid-flight, since .NET has no goroutine-count equivalent. Verified this test fails against the pre-fix code and passes with the fix. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
SteveSandersonMS
force-pushed
the
agent/close-failed-session-event-loop
branch
from
September 3, 2026 12:52
014be61 to
bcbf151
Compare
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Sep 3, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Sep 3, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Sep 3, 2026
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.
Summary
CreateSessionfailssession.destroyRPCRoot cause
newSessionstartsprocessEventsimmediately. The affected error paths removed the pre-registered session fromClient.sessionsbut left its event channel open. Because the failed session was neither returned to the caller nor retained forClient.Stop, its goroutine remained blocked on the channel for the lifetime of the process.The cleanup now removes only the session instance created by the failing call and closes its event channel without making another RPC.
Impact
Failed non-cloud session creation no longer leaks one goroutine, session object, and buffered event channel per attempt.
Validation
go test .go test -race . -run 'TestClient_CreateSession(Failure|InitializationFailure)ClosesRegisteredSession' -count=1git diff --checkFixes #2320