Skip to content

fix(main): share one window listener across overlapping requests to stop MaxListeners warnings - #33102

Merged
nwparker merged 3 commits into
mainfrom
nwparker/fix-webcontents-listener-leak
Oct 26, 2026
Merged

nwparker merged 3 commits into
mainfrom
nwparker/fix-webcontents-listener-leak

Conversation

@nwparker

@nwparker nwparker commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 15 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​238 $\color{#cf222e}{\Huge{\mathbf{−}}}$​41 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​197
Prod 10 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​143 $\color{#cf222e}{\Huge{\mathbf{−}}}$​68 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​75

ELI5

The main Orca window is like a shared bulletin board. Lots of short jobs (a file watch, a tab close, a remote-server subscription) pin a note on it saying "tell me if this window goes away". Each job did take its note down when it finished, but when many jobs ran at the same time the board filled up past its 10-note safety limit and Orca logged a "possible memory leak" warning. Now all those jobs share one note, and that note lists everyone who wants to know.

What Changed

Before: on a busy session (WSL host connected, renderer reloads, projects being moved) the main-process log filled with MaxListenersExceededWarning: ... 11 destroyed / render-process-gone / did-navigate listeners added to [WebContents] and 11 closed listeners added to [BrowserWindow]. Each in-flight request had added its own listener to the main window. The window already carries 7–9 permanent listeners per event from startup, so two or three overlapping requests were enough to cross the limit. Every reload or crash also walked one listener per open request.

After: short-lived watchers subscribe through a new addFanoutListener (src/main/event-listener-fanout.ts). It attaches one listener per window-and-event, keeps the interested callbacks in a set, and removes that listener when the last subscriber leaves. However many requests overlap, each window event costs one extra listener.

Callers moved onto it:

  • abortWhenRendererGone (src/main/ipc/renderer-lifetime-abort.ts): used by remote-runtime subscriptions, filesystem watchers, file mutations and downloads, native chat, emulator streams, log tail, media preview and dropped-path copies. This was the main source of the destroyed, render-process-gone and did-navigate warnings.
  • Terminal and session tab-close relays (src/main/window/*-tab-close-request-relay.ts). Session closes can stay pending for up to 5 minutes, so these kept closed, destroyed and render-process-gone listeners on the window. Their ipcMain reply listeners now share one listener too.
  • Project-move pause (src/main/window/project-move-pause.ts), plus the per-request destroyed watchers in SSH-access link, Jira summary generation and workspace-cleanup scans.

There is no user-visible behaviour change. Requests are still cancelled on exactly the same events.

Why

  • Raising setMaxListeners would only hide the warning. Listeners would still grow with the number of in-flight requests.
  • Making each job register more carefully wouldn't help. Every job already cleaned up. The warning came from overlapping jobs, not from forgotten listeners.
  • Merging the window's permanent startup listeners would only buy back a few slots and touches unrelated setup code. A shared fan-out caps per-request cost at one listener for every caller at once.

Linked Issue

Internal: found in Windows main-process logs from a WSL hosts build.

Visual Proof

N/A: main-process listener bookkeeping only. Nothing on screen changes.

Testing

  • New src/main/event-listener-fanout.test.ts: 25 overlapping subscribers share one listener, the count returns to zero after release, release during dispatch works, double release is safe, and the listener is re-attached after it drains.

  • New overlap tests on abortWhenRendererGone, both tab-close relays and project-move pause. Each starts 12–25 concurrent requests, checks there is exactly one listener per event (window, WebContents and ipcMain), then settles them all and checks the emitters return to zero listeners. Before this change those relay tests themselves printed MaxListenersExceededWarning.

  • Test doubles that only implemented once now implement on.

  • pnpm tc, oxlint on changed files and pnpm run check:code-quality:changed pass. All of src/main/ipc, src/main/window and src/main/media pass (639 files).

  • Platforms: macOS unit tests only. The warning was seen on Windows, but the code path has no platform branches.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

No wire or protocol changes, so mixed client/host versions are unaffected. SSH and WSL remote-runtime subscriptions benefit directly, because each one used to hold its own listeners. Cancellation semantics are unchanged.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 1 minute.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 190f5aba-4afc-45a4-bf6f-d31de1e63d14
Commits

Reviewing files that changed from the base of the PR and between bc47047 and 31e9eb0.

Files selected for processing: 25
  • src/main/event-listener-fanout.test.ts
  • src/main/event-listener-fanout.ts
  • src/main/ipc/filesystem/watchers/filesystem-watcher-local-events.test.ts
  • src/main/ipc/filesystem/watchers/filesystem-watcher-local-unsubscribe.test.ts
  • src/main/ipc/filesystem/watchers/filesystem-watcher-test-sender.ts
  • src/main/ipc/jira/issue-summary-generation.ts
  • src/main/ipc/native-chat-subscribe-lifecycle.test.ts
  • src/main/ipc/renderer-lifetime-abort.test.ts
  • src/main/ipc/renderer-lifetime-abort.ts
  • src/main/ipc/runtime-environments/runtime-environment-subscription-layout-recording.test.ts
  • src/main/ipc/runtime-environments/runtime-environments-subscription-lifecycle.test.ts
  • src/main/ipc/runtime-environments/runtime-environments-subscription-routing.test.ts
  • src/main/ipc/runtime-environments/runtime-environments-subscription-teardown.test.ts
  • src/main/ipc/runtime-ssh-access-handlers.ts
  • src/main/ipc/workspace-cleanup-snapshot-ipc.test.ts
  • src/main/ipc/workspace-cleanup.ts
  • src/main/media/media-preview-protocol.test.ts
  • src/main/media/media-preview-protocol.ts
  • src/main/window/dropped-path-preparation-ipc.test.ts
  • src/main/window/project-move-pause.test.ts
  • src/main/window/project-move-pause.ts
  • src/main/window/session-tab-close-request-relay.test.ts
  • src/main/window/session-tab-close-request-relay.ts
  • src/main/window/terminal-tab-close-request-relay.test.ts
  • src/main/window/terminal-tab-close-request-relay.ts

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8ebddd3e-7d0c-4017-8e47-dcae2e1943d5
Commits

Reviewing files that changed from the base of the PR and between c5e8b8b and bc47047.

Files selected for processing: 6
  • src/main/ipc/filesystem/watchers/filesystem-watcher-local-events.test.ts
  • src/main/ipc/filesystem/watchers/filesystem-watcher-local-unsubscribe.test.ts
  • src/main/ipc/filesystem/watchers/filesystem-watcher-test-sender.ts
  • src/main/window/project-move-pause.ts
  • src/main/window/session-tab-close-request-relay.test.ts
  • src/main/window/terminal-tab-close-request-relay.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

Walkthrough

The change adds utilities that share native event listeners across subscriptions and release them when subscriptions end. Renderer-lifecycle and request handlers use the utilities for cancellation, response, and cleanup events. Tests cover overlapping subscriptions and request lifecycles. Event-source types and test doubles are updated to use persistent on listeners.

Priority: Low

Merge Risk: Low · previous review bc470

The watcher test helper can hide a listener-cleanup regression. Correct the helper to restore confidence in those tests; no production failure is established by the remaining evidence.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 31.58% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 19 functions across 28 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: sharing window listeners across overlapping requests to prevent MaxListeners warnings.
Description check Passed The description is detailed and covers the change, rationale, testing, scope, and compatibility impact. It does not provide the required actual linked issue and leaves some checklist items unchecked, …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description is detailed and covers the change, rationale, testing, scope, and compatibility impact. It does not provide the required actual linked issue and leaves some checklist items unchecked, but it is otherwise mostly complete.

Finishing Touches: 💡 2
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fdb68287-aff5-40c7-9b30-d65e0aa9c595
Commits

Reviewing files that changed from the base of the PR and between 2a20d2d and c5e8b8b.

Files selected for processing: 25
  • src/main/event-listener-fanout.test.ts
  • src/main/event-listener-fanout.ts
  • src/main/ipc/filesystem-watcher-local-events.test.ts
  • src/main/ipc/filesystem-watcher-local-unsubscribe.test.ts
  • src/main/ipc/filesystem-watcher-test-sender.ts
  • src/main/ipc/jira/issue-summary-generation.ts
  • src/main/ipc/native-chat-subscribe-lifecycle.test.ts
  • src/main/ipc/renderer-lifetime-abort.test.ts
  • src/main/ipc/renderer-lifetime-abort.ts
  • src/main/ipc/runtime-environments/runtime-environment-subscription-layout-recording.test.ts
  • src/main/ipc/runtime-environments/runtime-environments-subscription-lifecycle.test.ts
  • src/main/ipc/runtime-environments/runtime-environments-subscription-routing.test.ts
  • src/main/ipc/runtime-environments/runtime-environments-subscription-teardown.test.ts
  • src/main/ipc/runtime-ssh-access-handlers.ts
  • src/main/ipc/workspace-cleanup-snapshot-ipc.test.ts
  • src/main/ipc/workspace-cleanup.ts
  • src/main/media/media-preview-protocol.test.ts
  • src/main/media/media-preview-protocol.ts
  • src/main/window/dropped-path-preparation-ipc.test.ts
  • src/main/window/project-move-pause.test.ts
  • src/main/window/project-move-pause.ts
  • src/main/window/session-tab-close-request-relay.test.ts
  • src/main/window/session-tab-close-request-relay.ts
  • src/main/window/terminal-tab-close-request-relay.test.ts
  • src/main/window/terminal-tab-close-request-relay.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

export function fireWatcherSenderEvent(sender: SenderEvents, event: string): void {
const removed = new Set(sender.removeListener.mock.calls.map(([, callback]) => callback))
for (const [name, callback] of sender.once.mock.calls) {
for (const [name, callback] of sender.on.mock.calls) {
if (name === event && !removed.has(callback)) {
sender.removeListener(name, callback)

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.

Minor · Functional Correctness · Quick win

Do not remove persistent listeners in the event helper.

SenderEvents.on now represents persistent registration, but fireWatcherSenderEvent calls removeListener before each callback. A cleanup assertion can pass even when the code under test does not release its listener. Repeated events also cannot reach a listener that should remain active. Let the callback or its owner perform removal.

Proposed fix
--- "a/src/main/ipc/filesystem-watcher-test-sender.ts"
+++ "b/src/main/ipc/filesystem-watcher-test-sender.ts"
@@ -20,10 +20,9 @@
 /** Fires the live (registered, not removed) `on` handlers for `event`, as Electron would. */
 export function fireWatcherSenderEvent(sender: SenderEvents, event: string): void {
   const removed = new Set(sender.removeListener.mock.calls.map(([, callback]) => callback))
   for (const [name, callback] of sender.on.mock.calls) {
     if (name === event && !removed.has(callback)) {
-      sender.removeListener(name, callback)
       callback()
     }
   }
 }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
sender.removeListener(name, callback)

@nwparker
nwparker force-pushed the nwparker/fix-webcontents-listener-leak branch from c5e8b8b to 6edce36 Compare October 26, 2026 19:27
…equests

Per-request lifecycle watchers (renderer-gone aborts, tab-close relays,
project-move pause, SSH-access link, Jira summary, workspace-cleanup scans)
each added their own destroyed / render-process-gone / did-navigate /
closed listener to the long-lived main window. They were removed on
settle, but with a dozen remote-runtime subscriptions or tab closes in
flight they stacked on top of the window's permanent listeners and tripped
MaxListenersExceededWarning.

addFanoutListener keeps one native listener per (emitter, event) and fans
out to a callback set, detaching when the last subscriber releases.
@nwparker
nwparker force-pushed the nwparker/fix-webcontents-listener-leak branch from bc47047 to 31e9eb0 Compare October 26, 2026 22:08
@nwparker
nwparker merged commit ba85f1a into main Oct 26, 2026
33 checks passed
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.

1 participant