Skip to content

perf(main): drop localhost label routes when a worktree is removed - #7557

Merged
nwparker merged 1 commit into
mainfrom
nwparker/leak-localhost-proxy-purge
Jul 6, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/leak-localhost-proxy-purge

Conversation

@nwparker

@nwparker nwparker commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Part of a full-codebase memory-leak audit — one never-purged per-entity structure per PR.

Description

The localhostWorktreeLabelProxy singleton (used by the opt-in "labeled localhost" feature that gives a dev server a stable *.orca.localhost hostname) keeps two module-level Maps: routes (label → route) and routeKeys (routeKey → label). registerRoute() was the only mutator — it only ever .set()s. There was no removal method on the class, and no worktree-teardown path touched the singleton, so a label route for a deleted worktree (or a since-changed dev-server port) survived in both maps for the entire app session.

Fix: add unregisterWorktree(worktreeId) and call it from removeWorktreeMetadataAndTransientState() — the single per-worktree main-process teardown function (8 call sites, right next to the existing advertisedUrlWatcher.forgetWorktree(...)).

Evidence

  • src/main/localhost-worktree-label-proxy.ts: routes/routeKeys mutated only via .set() in registerRoute (lines 44–45); grep of the whole class/file showed no delete/clear/unregister.
  • The only IPC surface (ipc/localhost-worktree-labels.ts) exposed register → registerRoute with no unregister counterpart.
  • Route key is worktree:{worktreeId}:{targetUrl} (shared/localhost-worktree-labels.ts), unique per worktree+URL, so a new worktree or a changed port permanently adds 2 entries that outlive worktree teardown.
  • Growth driver (with the off-by-default localhostWorktreeLabelsEnabled setting on): opening a labeled loopback dev-server link for a not-yet-seen worktree/port; creating and removing many worktrees over a long session accumulates entries.

Proof of fix

New test in localhost-worktree-label-proxy.test.ts: register routes for wt-a and wt-b, confirm both proxy through (200); unregisterWorktree('wt-a'); then wt-a's label returns 404 while wt-b still returns 200.

Test Files  1 passed (1)
      Tests  5 passed (5)

Typecheck (tsconfig.node.json) + oxlint clean.

ELI5

When you open a dev server with a friendly name, the app remembers "this friendly name points to that server." It remembered forever, even after you deleted the workspace that owned it. Now, when a workspace is removed, its friendly-name entries are thrown away too. The little name-forwarding server itself keeps running for everyone else.

Trade-offs

  • User-facing behavior: none. Removing a worktree already tears down its terminals, browser tabs, ports, and PR aliases; its labeled-localhost routes are dead the moment the worktree is gone, so dropping them changes nothing a user can observe.
  • Feature gating: the labeled-localhost feature is off by default; this path is a no-op when there are no routes for the worktree.
  • Scope: only routes whose worktreeId matches the removed worktree are dropped; routes registered without a worktreeId (legacy repo/project-name keys) are untouched — the renderer always supplies worktreeId. Target: 0 regression.

Made with Orca 🐋

LocalhostWorktreeLabelProxy.registerRoute added a permanent entry to routes
and routeKeys per (worktreeId, targetUrl) label, but the class had no removal
path and no worktree-teardown code touched the singleton — so routes for
deleted worktrees accumulated in both maps for the whole app session.

Add unregisterWorktree(worktreeId) and call it from
removeWorktreeMetadataAndTransientState (the single per-worktree main-process
teardown choke point, 8 call sites). The shared http.Server stays up.

Co-authored-by: Orca <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 434eb16f-1293-450c-83ae-4dc6d15b0a85

📥 Commits

Reviewing files that changed from the base of the PR and between b41cab1 and 6d870c4.

📒 Files selected for processing (3)
  • src/main/ipc/worktrees.ts
  • src/main/localhost-worktree-label-proxy.test.ts
  • src/main/localhost-worktree-label-proxy.ts

📝 Walkthrough

Walkthrough

This change adds an unregisterWorktree method to LocalhostWorktreeLabelProxy that removes all registered label routes associated with a given worktree ID from the proxy's internal route maps. This method is invoked from worktrees.ts during worktree metadata cleanup when a worktree is removed, ensuring stale route entries are not retained. A test verifies that unregistering one worktree's routes leaves other worktrees' routes intact.

Changes

File Summary
src/main/localhost-worktree-label-proxy.ts Added unregisterWorktree(worktreeId) method removing matching entries from routes and routeKeys.
src/main/ipc/worktrees.ts Imports the proxy and calls unregisterWorktree during metadata cleanup on worktree removal.
src/main/localhost-worktree-label-proxy.test.ts New test verifying selective route unregistration behavior.

Related PRs: None identified.

Suggested labels: bug, enhancement

Suggested reviewers: None identified.

Poem
A worktree gone, its label fades away,
No stale route lingers to lead requests astray.
The proxy sweeps its map both clean and true,
One id removed, the rest still shine through.
A rabbit hops, tests passing, work is through. 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers summary, evidence, and tests, but omits required template sections like Screenshots, AI Review Report, Security Audit, and Notes. Add the missing template sections, including Screenshots/No visual change, the testing checklist, AI review report, security audit, and Notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: removing localhost label routes when a worktree is removed.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

@nwparker
nwparker merged commit ec52db6 into main Jul 6, 2026
2 checks passed
@nwparker
nwparker deleted the nwparker/leak-localhost-proxy-purge branch July 6, 2026 23:43
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
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