Fix memory leak in AutoRepliesPtyServiceContribution on process dispose#312150
Open
winjo wants to merge 1 commit intomicrosoft:mainfrom
Open
Fix memory leak in AutoRepliesPtyServiceContribution on process dispose#312150winjo wants to merge 1 commit intomicrosoft:mainfrom
winjo wants to merge 1 commit intomicrosoft:mainfrom
Conversation
`handleProcessDispose` only cleared the inner auto-responders map and never removed entries from `_terminalProcesses` or `_autoResponders`. Every exited pty stayed referenced, retaining the `TerminalProcess`, its `UnixTerminal`, the `_socket` (`ReadStream`), and the underlying pipe file descriptors / kernel buffers. Symptoms: JS heap barely grows between snapshots, but RSS climbs by tens of MiB per batch of spawned+exited terminals because the native backing resources are held alive. Fix: also `delete(persistentProcessId)` from both outer maps at the end of `handleProcessDispose`, so each map's size tracks the number of live ptys.
Contributor
📬 CODENOTIFYThe following users are being notified based on files changed in this PR: @anthonykim1Matched files:
|
meganrogge
approved these changes
Apr 23, 2026
aeschli
approved these changes
Apr 23, 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.
The problem
AutoRepliesPtyServiceContribution.handleProcessDisposeonly clears the inner per-processMap<string, TerminalAutoResponder>of_autoResponders. It never removes the key from the outer_autoRespondersmap, and it never removes the corresponding entry from_terminalProcesses.ptyServicecallshandleProcessDispose(id)on everyTerminalProcess'sonProcessExit, so both maps grow monotonically for the lifetime of the pty host. Each leaked entry keeps aTerminalProcess→UnixTerminal→_socket(ReadStream) chain alive, which in turn keeps the pipe/pty file descriptors and their kernel read buffers allocated.How it shows up
ReadStream,TerminalProcess,UnixTerminal, andArrayBuffer/Uint32Arrayinstances.psRSS for the pty host grows by tens of MiB after a batch of spawned-then-exited terminals (we observed +70 MiB after a single task that spawned a few dozen pty-backed child processes).ReadStreamreliably walks back to_terminalProcesseson anAutoRepliesPtyServiceContributioninstance held byPtyService.The fix
Add the two missing
deletecalls at the end ofhandleProcessDispose, unconditionally (they must run even when_autoRespondershas no entry for the id, so they sit outside the existingifblock).After the fix,
_terminalProcesses.sizeand_autoResponders.sizetrack the number of live ptys, and the native fd/buffer memory is released as soon as the process exits.How to verify
ReadStream/TerminalProcess/UnixTerminalhave# Deleted = 0; after the fix,# New ≈ # Deleted.ps -o rss= <pty-host-pid>stops climbing proportionally to the number of closed terminals.