Skip to content

perf(ports): stop walking every /proc fd on each Linux port scan - #1

Closed
decrypted wants to merge 1 commit into
mainfrom
fix/linux-port-scan-inode-cache
Closed

decrypted wants to merge 1 commit into
mainfrom
fix/linux-port-scan-inode-cache

Conversation

@decrypted

@decrypted decrypted commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

ELI5

To show which dev servers belong to which workspace, Orca checks every 30 seconds which program owns each open port. On Linux it did that by looking through every open file of every program on the computer, each time. Now it skips ports owned by other users (it could never see those anyway), remembers who owned each port, and checks only that program again, unless something new shows up.

Files Added Deleted Net
Test 540 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​5787 $\color{#cf222e}{\Huge{\mathbf{−}}}$​29158 $\color{#cf222e}{\Huge{\mathbf{−}}}$​23371
Prod 2460 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​36440 $\color{#cf222e}{\Huge{\mathbf{−}}}$​23783 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​12657

What Changed

  • parseProcNetTcp also returns the socket's uid (column 8 of /proc/net/tcp). scanLinuxProcPorts only asks for owners of sockets whose uid is ours, or of all sockets when running as root. Other users' listeners are still listed, without a pid, as before.
  • New src/main/ports/linux-listener-socket-owners.ts replaces mapLinuxInodesToPids:
    • A remembered owner is reused only if that pid still holds the socket right now. That costs one /proc/<pid>/fd read instead of the whole system.
    • Sockets without a live owner trigger a walk of /proc from the highest pid down, which stops once all are found.
  • Walking from the highest pid down gives the same owner as before: the old walk overwrote its map in ascending order, so the last (highest) holder won. Shared sockets (prefork servers, a reloader and its child, systemd socket activation) keep the same owner. Because every scan re-proves its owners, the background scans and the fresh scan killWorkspacePort uses for Stop authorization return the same pid.

The ports list doesn't change. Only the cost of building it does.

Why

On a normal Linux desktop (~1,000 processes) every scan made ~12,300 sequential readlink calls and 1,029 readdir calls (555 of them failing with EACCES) on the main thread, to map ~45 listening sockets. About half of those sockets belong to other users (sshd, cupsd, systemd-resolved, …). Their fds are unreadable, so they never resolved, and the walk always covered every process. The main process went from ~2% to 50-65% of a core for ~0.5 s every 30 s.

Alternatives considered:

  • A retry backoff for unresolvable sockets (my first version). The uid filter removes the cause instead, and doesn't turn transient errors (EMFILE) into minutes without an owner.
  • A sync readlinkSync loop. Less CPU, but it blocks the main thread for tens of milliseconds.
  • Moving the walk to the port-scan worker. That worker only runs commands today. Doable, but it moves the cost instead of removing it.

Linked Issue

Fork PR. The upstream issue (draft ready) and the PR to stablyai/orca follow after review here.

Visual Proof

N/A: no UI or interaction change. The ports list shows the same listeners and owners.

Testing

  • Compared old and new owner resolution side by side on a live Linux desktop over 10 consecutive scans, including a listener that starts and one that exits mid-run: 0 mismatches against the old walk, and the uid filter lost no socket the old code could resolve.

  • A socket shared by a parent and its forked child resolves to the child (the highest pid), the same as the old walk, on both the first and cached scans.

  • CPU per scan, same machine and run: steady state 160-210 ms before, ~6 ms after. A new listener: ~170 ms before, 28 ms after. First scan after start: ~200 ms before, 91 ms after.

  • Measured the main process before the change: ~22-30k read syscalls and a 50-65% spike every 30 s (per-thread /proc/<pid>/task/<tid>/stat, V8 CPU profile via --inspect).

  • oxlint, oxfmt --check, pnpm test src/main/ports (7 files, 30 passed, 3 macOS-only skipped): all pass.

  • pnpm tc:node reports one error on current main unrelated to this PR: src/main/observability/git-execution-spans.ts imports shared/rpc-contract/git-admission-tier-params, which refactor(rpc): group request contracts by feature owner stablyai/orca#42063 moved to source-control/ while Attribute Git execution in field traces stablyai/orca#41950 added the import.

  • pnpm check:changed: 22 of 24 checks pass. Both failures happen without this PR: the typecheck error above, and src/main/runtime/terminal/launch/wsl-host-agent-launch-paths.test.ts > refuses a Windows-only agent (gets runtime_unavailable), which fails the same way with this commit reverted.

  • Platforms: Linux only. macOS and Windows code paths are untouched.

  • I manually tested these changes locally

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

  • Every added test fails without this change and avoids the test anti-patterns

No test added: the output is unchanged by design. The only test that would fail on revert counts /proc reads, which the anti-patterns rule out (work counts).

Risk

Level: Low

  • Riskiest part: the uid filter. A listening socket with another user's uid that is held by one of our processes (e.g. created before a privilege drop) now has no pid, where the old walk could find it. Root-owned Orca is unaffected: it resolves everything.
  • A remembered owner that keeps holding its socket stays the owner, even if a higher pid starts sharing it later (e.g. a new prefork worker). The old walk would have switched to the newest holder. The owner stays a real holder, and Stop authorization sees the same pid the panel shows.
  • Back out with a plain revert. No saved data or settings involved.

AI Disclosure

Written with Claude Code (Claude Opus 5.5): investigation, profiling and the patch. A separate AI code review pass changed the design (uid filter instead of backoff, highest-pid order). Reviewed and run by the author.

Review

  • Cross-platform: Linux-only code path. macOS (lsof) and Windows (netstat) are untouched.
  • SSH/remote: a Linux remote runtime reaches the same scanner through the workspace-ports RPC (runtime-workspace-port-operations.ts), so it gets the same change. The protocol and result shape are unchanged, so older paired hosts and clients aren't affected.
  • Agents/integrations: none.
  • Performance: ~25-30x less CPU per steady-state scan, with no main-thread blocking added.
  • UI: no change.
  • Security: Stop still authorizes against a scan that proves, right then, that the pid holds the listening socket. A remembered owner is never trusted without that check, so a reused pid can't inherit a socket.

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.

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)
  • I stated the risk: what could break, for whom, and how to back it out
  • 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)

🤖 Generated with Claude Code

@decrypted
decrypted force-pushed the fix/linux-port-scan-inode-cache branch from 8b4b883 to 53985f9 Compare November 16, 2026 07:29
@decrypted
decrypted force-pushed the fix/linux-port-scan-inode-cache branch 2 times, most recently from a1b7a08 to e9913d3 Compare November 18, 2026 08:42
decrypted added a commit that referenced this pull request Nov 18, 2026
Upstream 8abbc91..2f5abbc (245 commits). Branches rebased onto it:
fix/linux-port-scan-inode-cache (#1; now on upstream's linux-proc-net-listeners
parser, which gained a uid field), feat/port-detection-setting (#2),
feat/remote-autoconnect-setting (#3), feat/reduce-motion-setting (#4),
feat/keep-awake-timed (#5), feat/sleep-project-and-group (#6),
feat/project-group-filter (#7; group hider composed over upstream's
folder-nested exemption), fix/worktree-create-missing-base-hint (#8), ci/fork-build.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@decrypted
decrypted force-pushed the fix/linux-port-scan-inode-cache branch from e9913d3 to 31e1786 Compare November 19, 2026 08:18
decrypted added a commit that referenced this pull request Nov 19, 2026
Upstream 2f5abbc..7bc705a (345 commits). Branches rebased onto it:
#1 fix/linux-port-scan-inode-cache, #2 feat/port-detection-setting, #3 feat/remote-autoconnect-setting,
#4 feat/reduce-motion-setting, #5 feat/keep-awake-timed, #6 feat/sleep-project-and-group,
#7 feat/project-group-filter, #8 fix/worktree-create-missing-base-hint; plus ci/fork-build.

- #2: upstream stablyai#43836 took the refreshing flag away from the background scanner; the
  detection-off reset no longer clears it.
- #4: upstream's HoverScrollText and the composer glow use the motion-safe / motion-reduce
  variants; the bot activity list scrolls with motionSafeScrollBehavior.
- #7: kept upstream's bot-listed workspace exclusion next to the project-group scope.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@decrypted
decrypted force-pushed the fix/linux-port-scan-inode-cache branch from 31e1786 to 87a9908 Compare November 20, 2026 08:21
decrypted added a commit that referenced this pull request Nov 20, 2026
Upstream 7bc705a..3df243e (480 commits), plus #1-#8 rebased onto it
and ci/fork-build. Tree verified with tc + check:changed (42 checks).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Linux port scan mapped listening sockets to pids by reading every fd
of every process on the machine, every 30 s, on the main thread. Sockets
owned by other users never resolve (their fds are unreadable), so the
walk never stopped early.

Only resolve sockets whose uid (from /proc/net/tcp) is ours, or all of
them when running as root. Remember each socket's owner and reuse it only
while that pid still holds the socket, which costs one fd list instead of
the whole system. Walk /proc only for sockets without a live owner, from
the highest pid down, and stop once all are found: that keeps the old
walk's result (the last, i.e. highest, holder) and reaches a new
listener's process first.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@decrypted
decrypted force-pushed the fix/linux-port-scan-inode-cache branch from 87a9908 to 3b86c41 Compare November 23, 2026 08:18
decrypted added a commit that referenced this pull request Nov 24, 2026
Retires fix/linux-port-scan-inode-cache (#1): upstream stablyai#47943
moved the Linux socket-owner lookup off the main process (one ls in the
port-scan worker), which covers what #1 was for.

#2 follows upstream stablyai#47994's forward owners (SSH or paired server) in the
detection-off panel; its one-off scan guard moved into the store's
replaceWorkspacePortScans (workspace-port-actions.ts hit max-lines).
#4 routes the new BotItemReplyJump reveal through the Reduce motion helper.
@decrypted

Copy link
Copy Markdown
Owner Author

Retiring this in favour of upstream stablyai#47943 ("Move the Linux port scan's per-file process lookup off Orca's main process"), merged 2026-11-24 (upstream 2af98f8).

Upstream now maps listening sockets to pids with one ls -lnq /proc/[0-9]*/fd in the port-scan worker instead of one readlink promise per fd on Electron main. Kill and allow-list checks still walk exactly. That removes the main-process cost this PR was for. Measured here: 882 processes, ~18k fds, ~0.12 s CPU per 30 s scan, in a child process.

Upstream tried a reuse cache like this one first and dropped it: a socket handed to a child while its parent lives keeps the cached owner. Keeping this PR on top would bring that back, for a small remaining CPU saving.

The branch fix/linux-port-scan-inode-cache stays (at 3b86c41) in case it's wanted again. local/build fb9bdd1 no longer contains it.

@decrypted decrypted closed this Nov 24, 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