Skip to content

fix pty output make main process oom - #285419

Closed
theanarkh (theanarkh) wants to merge 2 commits into
microsoft:mainfrom
theanarkh:fix_pty_output_make_main_process_oom
Closed

theanarkh (theanarkh) wants to merge 2 commits into
microsoft:mainfrom
theanarkh:fix_pty_output_make_main_process_oom

Conversation

@theanarkh

@theanarkh theanarkh (theanarkh) commented Dec 30, 2025 •

Copy link
Copy Markdown
Contributor

Fixes #234393

The local pty process communicates directly with the render process, but the main process save the output of pty process to buffer and there is no consumer.

Running

node -e "setInterval(() => console.log('x'.repeat(100)), 10)"

in the terminal and take a JS heap snapshot of main process.

image

take JS heap snapshot again after some time.

image

Copilot AI review requested due to automatic review settings December 30, 2025 12:43
@vs-code-engineering

vs-code-engineering Bot commented Dec 30, 2025 •

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

Robo (@deepak1556)

Matched files:

  • src/vs/code/electron-main/app.ts

@theanarkh

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a memory leak (OOM) in the main Electron process caused by unbounded buffering of PTY process output. The local PTY process communicates directly with the renderer process via MessagePort, but the main process was also buffering all onProcessData events even though they were never consumed, leading to memory exhaustion.

Key Changes:

  • Added a skip option to ProxyChannel.fromService to selectively exclude events from registration
  • Applied the skip option to exclude onProcessData events from the main process PTY service channel

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/vs/base/parts/ipc/common/ipc.ts Added skip option to ICreateServiceChannelOptions interface to allow selective event registration filtering
src/vs/code/electron-main/app.ts Applied skip option to exclude onProcessData from the PTY host channel registration in main process

Comment thread src/vs/base/parts/ipc/common/ipc.ts Resolved
Comment thread src/vs/base/parts/ipc/common/ipc.ts Resolved
Comment thread src/vs/base/parts/ipc/common/ipc.ts Resolved

@Tyriar Daniel Imms (Tyriar) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm thanks!

FYI João Moreno (@joaomoreno) Alexandru Dima (@alexdima) if you have thoughts on ipc/ parts

* If return true for a event key, we add the handler to event
* until the first listener is registered.
*/
isLazyEvent?: (key: string) => boolean;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am reading this comment and fail to understand what actually happens when I use this option. If I understand correctly, a "lazy" event will not buffer any data until a first listener is added. But then I would update this comment to actually explain this better. And should be called differently?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I have updated the comment, please help review again .

// Terminal
const ptyHostChannel = ProxyChannel.fromService(accessor.get(ILocalPtyService), disposables);
const ptyHostChannel = ProxyChannel.fromService(accessor.get(ILocalPtyService), disposables, {
isLazyEvent: (key: string) => key === 'onProcessData'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats the rule here to decide if an event should be lazy? Does this apply to other proxy channels?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing ! An event should be lazy if it is triggered automatically but may not have subscribers; otherwise, the event data will be cached in the buffer (Event.buffer), causing a memory leak. I only find that ptyHostService.onProcessData should be lazy currently.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

theanarkh (@theanarkh) I think what Benjamin Pasero (@bpasero) is likely getting at is this should get fixed generically to prevent future leaks like this in the future. onProcessData is used, but conditionally, just because an event isn't listened to shouldn't mean the data should hang around forever. Any thoughts on how we could solve this generically for all events without hardcoding this particular event or compromising performance?

@theanarkh theanarkh (theanarkh) Jan 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onProcessData is used in both local and remote scenarios. Only in the local scenario it have no subscribers. Therefore, we cannot directly configure onProcessData event to skip event buffer. Instead, we configure it in the caller of fromService. I haven't thought of a general way to deal with this problem currently.

auto-merge was automatically disabled December 31, 2025 16:52

Head branch was pushed to by a user without write access

@theanarkh
theanarkh (theanarkh) force-pushed the fix_pty_output_make_main_process_oom branch from 4eaa235 to 3415bc2 Compare December 31, 2025 16:52
@Tyriar
Daniel Imms (Tyriar) marked this pull request as draft January 1, 2026 11:27

@joaomoreno João Moreno (joaomoreno) left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also not a big fan of introducing a lazy concept. A few alternatives:

  • Since the proxy stuff is all just-works-magic, why not having a decorator on the method like @IPCNoBuffer or something, instead of string based matching?
  • Avoid the magic proxy and use manually crafted channels (example) for the ptyhost, the old fashioned way.

@alexdima

Alexandru Dima (alexdima) commented Feb 28, 2026 •

Copy link
Copy Markdown
Member

João Moreno (@joaomoreno) I've prepared #298468 which should point us to the other leaking places (3-4 right now). The question I have is ... why the usage of Event.buffer here? Normally, events can be missed.

I also think this way of using Event.buffer once in the main process doesn't work correctly for 2 windows. The first window will come and take all the buffered events while the second window is left with missing them out. So I think we should remove the Event.buffer usage since only the first window that connects benefits from the buffering anyways.

@dmitrivMS

Copy link
Copy Markdown
Collaborator

Superseded by #328535

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codes Crashes after about 15 Minutes

10 participants