fix(terminal-chat): dedupe terminal tool-session registrations to prevent listener leak (#309906)#310740
Merged
meganrogge merged 2 commits intomicrosoft:mainfrom Apr 17, 2026
Conversation
…vent listener leak (microsoft#309906)
Yoyokrazy
approved these changes
Apr 16, 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.
What
Dedupe terminal tool-session registrations in
TerminalChatService.registerTerminalInstanceWithToolSessionso repeated registrations for the same terminal instance don't stack up event listeners.Why
RunInTerminalTool.invoke()generates a freshterminalToolSessionIdper tool call and re-registers the same terminal instance each time. The existing implementation subscribed tochatService.onDidDisposeSessionandinstance.onDisposedon every call without disposing the prior listeners, so listeners accumulated on shared emitters until the leak detector tripped. This is the root cause behind the telemetry bucket[Unhandled Error] potential listener LEAK detected, dominatedreported in #309906 (error-telemetry, recent-regression).How
Added a dedupe guard at the top of
registerTerminalInstanceWithToolSession:terminalToolSessionId, early-return (no-op).deleteAndDisposethe stale listener and drop the stale mapping before registering the new one.This mirrors the pattern already used by the sibling
registerTerminalInstanceWithChatSessionin the same file, keeping the two registration paths consistent.Test plan
Added
src/vs/workbench/contrib/terminalContrib/chat/test/browser/terminalChatService.test.tswith two regression tests that inspectEmitter._size(the same counter the leak detector uses):onDidDisposeSessionstays ≤ 1 after 50 re-registrations with fresh tool-session ids.npm run compile-check-ts-nativepasses cleanly for this change.Fixes #309906