From 484cdc02e52ff39ea0fcee727df1ee1c250c69da Mon Sep 17 00:00:00 2001 From: Neil Date: Tue, 10 Nov 2026 22:12:03 -0800 Subject: [PATCH] fix(w3): read /proc ancestry asynchronously (main-sync-fs ratchet) --- src/main/ports/host-managed-listeners.test.ts | 54 +++++++++++-------- src/main/ports/host-managed-listeners.ts | 19 ++++--- .../ports/local-workspace-port-scanner.ts | 5 +- 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/src/main/ports/host-managed-listeners.test.ts b/src/main/ports/host-managed-listeners.test.ts index f60b881ad05..3ba814d2776 100644 --- a/src/main/ports/host-managed-listeners.test.ts +++ b/src/main/ports/host-managed-listeners.test.ts @@ -7,7 +7,7 @@ const config = (roots: string[] = ['/opt/orca'], unownedPorts: number[] = [23456 }) describe('host-managed listeners', () => { - it('reads absolute roots and platform ports from the environment', () => { + it('reads absolute roots and platform ports from the environment', async () => { const parsed = hostManagedListenerConfig({ ORCA_HOST_MANAGED_LISTENER_PATHS: '/opt/orca/:relative:/srv/tools', ORCA_HOST_MANAGED_UNOWNED_PORTS: '23456, x,70000' @@ -17,17 +17,17 @@ describe('host-managed listeners', () => { expect(hostManagedListenerConfig({})).toEqual({ roots: [], unownedPorts: new Set() }) }) - it('marks the server itself and programs started from a managed root', () => { - expect(isHostManagedListener({ pid: 27, port: 6768 }, config(), 27)).toBe(true) + it('marks the server itself and programs started from a managed root', async () => { + expect(await isHostManagedListener({ pid: 27, port: 6768 }, config(), 27)).toBe(true) expect( - isHostManagedListener( + await isHostManagedListener( { pid: 90, port: 2222, commandLine: '/opt/orca/openssh/sbin/sshd -D -f x' }, config(), 27 ) ).toBe(true) expect( - isHostManagedListener( + await isHostManagedListener( { pid: 91, port: 6080, @@ -39,44 +39,52 @@ describe('host-managed listeners', () => { ).toBe(true) }) - it('marks a pid-less listener only on a declared platform port', () => { - expect(isHostManagedListener({ port: 23456 }, config(), 27)).toBe(true) + it('marks a pid-less listener only on a declared platform port', async () => { + expect(await isHostManagedListener({ port: 23456 }, config(), 27)).toBe(true) // A root-run postgres or Docker-published port has no visible pid but is the user's. - expect(isHostManagedListener({ port: 5432 }, config(), 27)).toBe(false) - expect(isHostManagedListener({ port: 23456 }, config(['/opt/orca'], []), 27)).toBe(false) + expect(await isHostManagedListener({ port: 5432 }, config(), 27)).toBe(false) + expect(await isHostManagedListener({ port: 23456 }, config(['/opt/orca'], []), 27)).toBe(false) }) - it('leaves user servers alone, matching by program path rather than port', () => { + it('leaves user servers alone, matching by program path rather than port', async () => { expect( - isHostManagedListener( + await isHostManagedListener( { pid: 92, port: 6768, commandLine: 'python3 -m http.server 6768' }, config(), 27 ) ).toBe(false) expect( - isHostManagedListener( + await isHostManagedListener( { pid: 92, port: 23456, commandLine: 'python3 -m http.server 23456' }, config(), 27 ) ).toBe(false) expect( - isHostManagedListener( + await isHostManagedListener( { pid: 93, port: 5173, commandLine: 'node vite --config /opt/orca/x.js' }, config(), 27 ) ).toBe(false) expect( - isHostManagedListener({ pid: 94, port: 1, commandLine: '/opt/orcaother/bin/x' }, config(), 27) + await isHostManagedListener( + { pid: 94, port: 1, commandLine: '/opt/orcaother/bin/x' }, + config(), + 27 + ) ).toBe(false) expect( - isHostManagedListener({ pid: 95, port: 1, commandLine: '/opt/orca/bin/x' }, config([]), 27) + await isHostManagedListener( + { pid: 95, port: 1, commandLine: '/opt/orca/bin/x' }, + config([]), + 27 + ) ).toBe(false) }) - it('walks ancestry to a managed tool, stopping at the server and at shells', () => { + it('walks ancestry to a managed tool, stopping at the server and at shells', async () => { const parents: Record = { 985: { pid: 941, commandLine: '/opt/orca/runtime/orcad/agent-browser-linux-x64' }, 1018: { pid: 985, commandLine: '/usr/bin/google-chrome --headless' }, @@ -86,25 +94,25 @@ describe('host-managed listeners', () => { 500: { pid: 499, commandLine: '-bash' }, 499: { pid: 498, commandLine: '/opt/orca/openssh/sbin/sshd -D -f x' } } - const read = (pid: number) => parents[pid] + const read = async (pid: number) => parents[pid] const chrome = { pid: 985, port: 42063, commandLine: '/usr/bin/google-chrome --headless' } - expect(isHostManagedListener(chrome, config(), 27, read)).toBe(true) + expect(await isHostManagedListener(chrome, config(), 27, read)).toBe(true) // Grandchild: a Chrome helper two levels below agent-browser. expect( - isHostManagedListener( + await isHostManagedListener( { pid: 1018, port: 42064, commandLine: '/opt/google/chrome/chrome --type=utility' }, config(), 27, read ) ).toBe(true) - expect(isHostManagedListener(chrome, config([]), 27, read)).toBe(false) + expect(await isHostManagedListener(chrome, config([]), 27, read)).toBe(false) const user = (pid: number) => ({ pid, port: 8123, commandLine: 'python3 -m http.server' }) // `exec python3 -m http.server` in a terminal: its parent is the server. - expect(isHostManagedListener(user(300), config(), 27, read)).toBe(false) + expect(await isHostManagedListener(user(300), config(), 27, read)).toBe(false) // Terminal shell under the server. - expect(isHostManagedListener(user(400), config(), 27, read)).toBe(false) + expect(await isHostManagedListener(user(400), config(), 27, read)).toBe(false) // An SSH session's shell under the managed sshd. - expect(isHostManagedListener(user(500), config(), 27, read)).toBe(false) + expect(await isHostManagedListener(user(500), config(), 27, read)).toBe(false) }) }) diff --git a/src/main/ports/host-managed-listeners.ts b/src/main/ports/host-managed-listeners.ts index 64de9feae18..396270e307f 100644 --- a/src/main/ports/host-managed-listeners.ts +++ b/src/main/ports/host-managed-listeners.ts @@ -1,4 +1,4 @@ -import { readFileSync } from 'node:fs' +import { readFile } from 'node:fs/promises' import type { RawListeningPort } from './local-workspace-port-scan-state' /** Colon-separated install roots whose binaries are the host's own services (e.g. a sandbox image's tools). */ @@ -33,12 +33,12 @@ export function hostManagedListenerConfig( * declared platform port whose owner is invisible. A pid-less listener on any other port (a root-run postgres or * Docker-published port) is still the user's. */ -export function isHostManagedListener( +export async function isHostManagedListener( port: Pick, config: HostManagedListenerConfig, serverPid: number = process.pid, - readParent: (pid: number) => ParentProcess | undefined = readLinuxParentProcess -): boolean { + readParent: (pid: number) => Promise = readLinuxParentProcess +): Promise { if (port.pid === undefined) { return config.unownedPorts.has(port.port) } @@ -55,7 +55,7 @@ export function isHostManagedListener( // (it spawns the user's terminals) and at a shell (a user session, e.g. over the managed sshd). let pid = port.pid for (let depth = 0; depth < MAX_ANCESTRY_DEPTH; depth++) { - const parent = readParent(pid) + const parent = await readParent(pid) if (!parent || parent.pid === serverPid || isShell(parent.commandLine)) { return false } @@ -85,18 +85,21 @@ function startedFromRoot(commandLine: string | undefined, roots: readonly string .some((token) => roots.some((root) => token === root || token.startsWith(`${root}/`))) } -function readLinuxParentProcess(pid: number): ParentProcess | undefined { +async function readLinuxParentProcess(pid: number): Promise { if (process.platform !== 'linux') { return undefined } try { // /proc//stat: "pid (comm) state ppid ..."; comm may contain spaces or parens. - const stat = readFileSync(`/proc/${pid}/stat`, 'utf8') + const stat = await readFile(`/proc/${pid}/stat`, 'utf8') const ppid = Number(stat.slice(stat.lastIndexOf(')') + 2).split(' ')[1]) if (!Number.isInteger(ppid) || ppid <= 1) { return undefined } - const commandLine = readFileSync(`/proc/${ppid}/cmdline`, 'utf8').split('\0').join(' ').trim() + const commandLine = (await readFile(`/proc/${ppid}/cmdline`, 'utf8')) + .split('\0') + .join(' ') + .trim() return { pid: ppid, commandLine } } catch { return undefined diff --git a/src/main/ports/local-workspace-port-scanner.ts b/src/main/ports/local-workspace-port-scanner.ts index 5b85224fda5..7b370e9b421 100644 --- a/src/main/ports/local-workspace-port-scanner.ts +++ b/src/main/ports/local-workspace-port-scanner.ts @@ -42,9 +42,12 @@ export async function scanWorkspacePorts( options.requireMetadata ) const hostManaged = hostManagedListenerConfig() + const managedFlags = await Promise.all( + rawPorts.map((port) => isHostManagedListener(port, hostManaged)) + ) const managed = new Set( rawPorts - .filter((port) => isHostManagedListener(port, hostManaged)) + .filter((_, index) => managedFlags[index]) .map((port) => `${port.host}:${port.port}:${port.pid ?? 'unknown'}`) ) // Orca's own listeners (runtime socket, hook server) run with a workspace in their command line but are not its servers.