Skip to content

fix(browser): avoid repeated native port cleanup inventories - #35346

Merged
OrcaWin merged 1 commit into
mainfrom
OrcaWin/native-port-frame-count-independent
Oct 29, 2026
Merged

OrcaWin merged 1 commit into
mainfrom
OrcaWin/native-port-frame-count-independent

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 0 0 0 0
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​8 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​7

ELI5

When an extension closes many native messaging ports together, Orca repeatedly copies the remaining port list just to decide whether a timer is still needed. Track how many frame-owned ports are open so cleanup does only the required close work.

Change

Maintain one module-local count alongside the existing port map. Increment at the sole admission point and decrement inside the existing idempotent finish guard, before callbacks. Stop the existing frame sweep when the count reaches zero, after callbacks as before. A synchronously admitted successor keeps the timer alive.

This removes the full-registry spread and search from every finish. Using an early-exit loop would still repeatedly scan surviving worker ports. The existing live-map cleanup traversal, host close order, worker task ownership, event order, timer cadence and error behavior stay in their current implementations. Pre-existing since native messaging was introduced in #30940.

Measurements

Actual production-module execution, controlled native boundaries, registered extension-unload and WebContents-destroyed handlers:

Closing frame ports Extra copied entries before → after Total visited entries before → after
1 0 → 0 1 → 1
10 45 → 0 55 → 10
100 4,950 → 0 5,050 → 100

Both lifecycle routes produce these counts. Before, N closes allocate N arrays and copy N(N−1)/2 entries. After, the required N-entry cleanup remains and extra copies are zero.

Closing the last frame while 100 workers survive copies 100 records before and zero after, with identical worker survival and timer release. Healthy sweeps still visit N records and close nothing; worker-only state starts no sweep.

Validation

  • 26 before/after scenarios preserve full functional traces: host starts/closes, messages, task endings, disconnect errors/teardown flags, callback order and timer start/clear order.
  • Seven zero-rescan structural postconditions fail on exact base and pass with the change.
  • Controls include siblings/sessions/extensions, worker versions, failed/canceled lookup, pending/failed host start, early host close, repeated/late finish, navigation/crash/sweep, reentrant successor admission, sibling close, map mutation during teardown and throwing callbacks.
  • Nine existing native-port tests pass on both versions. Final focused preload/main-world/native-port/host/framing suites pass 42 tests across five files.
  • Node typecheck, formatting, diff check and all nine changed-code quality groups pass. The initial command-name typo is archived; it ran no quality checks. No precommit-hook claim; standalone setup did not prepare Husky.
  • All 81 production source inputs match the measured proposal. The final production file matches its exact sealed SHA256.
  • Required ORCA_BACKGROUND_LAUNCH=1 pnpm check:changed --maxWorkers=2: author run passed 19/19 checks in 173.5s on 1e19fa6 (vs origin/main bbd635e). After review rebase onto origin/main bbb7f2d (clean, no upstream changes to native-port files, no code change), it passed 20/20 checks in 131.3s on 87b5cad.

Scope and evidence

The actual port registry, admission, finish, registered lifecycle handlers and callbacks run from source in isolated Node VM contexts. Electron/session access, worker-state lookup, host lookup and host start are controlled. A native Map subclass counts yielded registry entries; the single registry-construction statement is instrumented equally in both arms. No production function or lifecycle handler is replaced. Zero windows or native host processes are launched.

This proves work counts and preserved lifecycle behavior. It does not measure CPU, physical heap, latency, full Orca UI, actual host termination, installed extensions or native macOS/Windows behavior. The module is desktop/session-owned; no remote protocol, mobile behavior or git/folder-workspace assumptions change.

Independent tmpfs author clone, frozen-lock dependencies and verified pinned Bun 1.4.4. Evidence: /home/neil/.local/state/orca-electron-audit/native-port-frame-count-candidate/report.md, 24-artifact seal, baseline/fixed traces and operation counts, and implementation/ source verification, logs and commit bundle. No new checked-in implementation-mirroring test was added.

@OrcaWin
OrcaWin marked this pull request as ready for review October 29, 2026 00:36
@OrcaWin
OrcaWin force-pushed the OrcaWin/native-port-frame-count-independent branch from 1e19fa6 to 87b5cad Compare October 29, 2026 00:36
@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

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 6 seconds.

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: c8cf0991-ce70-4e6a-a36d-306f0e52a043
Commits

Reviewing files that changed from the base of the PR and between 6f41a55 and 87b5cad.

Files selected for processing: 1
  • src/main/browser/extensions/browser-extension-native-ports.ts
  • 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.

@greptile-apps

greptile-apps Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Replaces the per-close copy and search of the port map with framePortCount.

  • Frame-port cleanup stops scanning the full port map.

Reviews (2) · Last reviewed commit: "fix(browser): track native frame ports d..." · Reviewed by Greptile

@OrcaWin
OrcaWin marked this pull request as draft October 29, 2026 00:48
@OrcaWin
OrcaWin marked this pull request as ready for review October 29, 2026 00:49
@OrcaWin
OrcaWin merged commit 8e0c153 into main Oct 29, 2026
109 of 111 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