The best way to control Claude Code, Codex and more from any computer or phone
perf(server): avoid lsof polling when Linux proc is usable (#30011)
Lakshmi Tanmay committed
Nov 7, 2026 at 16:07 UTC
bc46c0ff682395baa5c1ef1093571bc21d28d51d
2 files changed
+177
-26
apps/server/src/preview/PortScanner.test.ts
+135
-7
@@ -436,17 +436,26 @@ const PROC_NET_TCP6 = ` sl local_address remote_addres
436
`;
437
438
effectIt.effect(
439
- "reads Linux listeners from /proc when lsof is missing, and stops spawning it",
439
+ "prefers Linux /proc over installed lsof, and bounds repeated socket owner lookups",
440
() => {
441
let lsofSpawns = 0;
442
const fdWalks: string[] = [];
443
const procFiles: Record<string, string> = {
444
- "/proc/net/tcp": PROC_NET_TCP,
444
+ "/proc/net/tcp":
445
+ PROC_NET_TCP +
446
+ PROC_NET_TCP.split("\n")[1]!
447
+ .replace("0100007F:223D", "0101007F:1388")
448
+ .replace("5001", "5007"),
449
"/proc/net/tcp6": PROC_NET_TCP6,
450
"/proc/4242/comm": "python3\n",
451
};
452
const procFds: Record<string, Record<string, string>> = {
449
- "4242": { "0": "/dev/null", "3": "socket:[5001]" },
453
+ "4242": {
454
+ "0": "/dev/null",
455
+ "3": "socket:[5001]",
456
+ "4": "socket:[5004]",
457
+ "5": "socket:[5007]",
458
+ },
459
"77": { "5": "socket:[9999]" },
460
};
461
const missing = FileSystem.makeNoop({});
@@ -475,9 +484,9 @@ effectIt.effect(
484
return Promise.resolve(new Response("hello", { headers: { "content-type": "text/html" } }));
485
}) as typeof globalThis.fetch;
486
const layer = layerProbeFailure(
478
- (input) => {
487
+ () => {
488
lsofSpawns += 1;
480
- return processProbeFailure(input);
489
+ return Effect.succeed(processOutput("p4242\ncpython3\nn*:3001\nn*:8765\n"));
490
},
491
fetchFn,
492
fileSystem,
@@ -488,18 +497,137 @@ effectIt.effect(
497
const first = yield* scanner.scan();
498
expect(
499
first.map(({ port, pid, processName, url }) => ({ port, pid, processName, url })),
491
- ).toEqual([{ port: 8765, pid: 4242, processName: "python3", url: "http://localhost:8765" }]);
500
+ ).toEqual([
501
+ { port: 3001, pid: 4242, processName: "python3", url: "http://localhost:3001" },
502
+ { port: 8765, pid: 4242, processName: "python3", url: "http://localhost:8765" },
503
+ ]);
504
yield* scanner.scan();
493
- expect(lsofSpawns).toBe(1);
505
+ expect(lsofSpawns).toBe(0);
506
expect(fdWalks).toEqual(["77", "4242", "77", "4242"]);
507
yield* scanner.scan();
508
yield* scanner.scan();
509
expect(fdWalks).toEqual(["77", "4242", "77", "4242", "77", "4242"]);
510
+ procFds["77"]!["6"] = "socket:[5006]";
511
+ procFiles["/proc/net/tcp6"] =
512
+ PROC_NET_TCP6 + PROC_NET_TCP.split("\n")[1]!.replace("5001", "5006");
513
+ expect((yield* scanner.scan()).map(({ port }) => port)).toEqual([3001]);
514
+ const walksAfterCollision = fdWalks.length;
515
+ procFiles["/proc/net/tcp"] = PROC_NET_TCP.split("\n")[0]!;
516
+ procFiles["/proc/net/tcp6"] = PROC_NET_TCP6.split("\n")[0]!;
517
+ expect(yield* scanner.scan()).toEqual([]);
518
+ expect(lsofSpawns).toBe(0);
519
+ expect(fdWalks).toHaveLength(walksAfterCollision);
520
expect(requests).toEqual([]);
521
+
522
+ procFiles["/proc/net/tcp"] = PROC_NET_TCP;
523
+ procFiles["/proc/net/tcp6"] = PROC_NET_TCP6;
524
+ expect((yield* scanner.scan()).map(({ pid }) => pid)).toEqual([4242, 4242]);
525
+ yield* scanner.retain.pipe(Effect.scoped);
526
+ procFds["77"] = procFds["4242"]!;
527
+ delete procFds["4242"];
528
+ procFiles["/proc/77/comm"] = "python3\n";
529
+ yield* scanner.registerTerminalProcesses({
530
+ threadId: "scanner-thread",
531
+ terminalId: "scanner-terminal",
532
+ processIds: [77],
533
+ });
534
+ expect((yield* scanner.scan()).map(({ port, pid }) => ({ port, pid }))).toEqual([
535
+ { port: 3001, pid: 77 },
536
+ { port: 8765, pid: 77 },
537
+ ]);
538
}).pipe(Effect.provide(layer));
539
},
540
);
541
542
+effectIt.effect.each([
543
+ { mode: "missing", fallback: true },
544
+ { mode: "denied", fallback: true },
545
+ { mode: "invalid", fallback: true },
546
+ { mode: "fd-unavailable", fallback: true },
547
+ { mode: "fd-budget", fallback: true },
548
+ { mode: "fd-budget-no-lsof", fallback: true },
549
+ { mode: "ipv6-denied", fallback: true },
550
+ { mode: "ipv6-absent", fallback: false },
551
+ { mode: "empty", fallback: false },
552
+ { mode: "darwin", fallback: true },
553
+] as const)("uses lsof only if Linux /proc is unusable: $mode", ({ mode, fallback }) => {
554
+ let lsofSpawns = 0;
555
+ let procWalks = 0;
556
+ const missing = FileSystem.makeNoop({});
557
+ const layer = PortScanner.layer.pipe(
558
+ Layer.provide(
559
+ Layer.mergeAll(
560
+ FileSystem.layerNoop({
561
+ readFileString: (path) => {
562
+ if (!path.startsWith("/proc/net/tcp") || mode === "missing") {
563
+ return missing.readFileString(path);
564
+ }
565
+ if (mode === "denied" || (mode === "ipv6-denied" && path.endsWith("tcp6"))) {
566
+ return Effect.fail(
567
+ PlatformError.systemError({
568
+ _tag: "PermissionDenied",
569
+ module: "FileSystem",
570
+ method: "readFileString",
571
+ }),
572
+ );
573
+ }
574
+ if (mode === "ipv6-absent" && path.endsWith("tcp6")) {
575
+ return missing.readFileString(path);
576
+ }
577
+ if (mode === "invalid") return Effect.succeed("unusable table");
578
+ return Effect.succeed(
579
+ mode === "fd-unavailable" || mode.startsWith("fd-budget")
580
+ ? PROC_NET_TCP
581
+ : PROC_NET_TCP.split("\n")[0]!,
582
+ );
583
+ },
584
+ readDirectory: (path) => {
585
+ if (!mode.startsWith("fd-budget")) return missing.readDirectory(path);
586
+ if (path === "/proc") procWalks += 1;
587
+ if (mode === "fd-budget-no-lsof") {
588
+ if (path === "/proc") return Effect.succeed(["1234", "9999"]);
589
+ if (path === "/proc/1234/fd") return Effect.succeed(["3"]);
590
+ }
591
+ return Effect.succeed(
592
+ path === "/proc"
593
+ ? ["1", "1234"]
594
+ : Array.from({ length: 50_000 }, (_, i) => String(i)),
595
+ );
596
+ },
597
+ readLink: (path) =>
598
+ mode === "fd-budget-no-lsof" && path === "/proc/1234/fd/3"
599
+ ? Effect.succeed("socket:[5001]")
600
+ : mode.startsWith("fd-budget")
601
+ ? Effect.succeed("/dev/null")
602
+ : missing.readLink(path),
603
+ }),
604
+ Layer.succeed(ProcessRunner.ProcessRunner, {
605
+ run: (input) => {
606
+ lsofSpawns += 1;
607
+ if (mode === "fd-budget-no-lsof") return processProbeFailure(input);
608
+ return Effect.succeed(processOutput(`p1234\ncnode\nn*:${LSOF_TEST_PORT}\n`));
609
+ },
610
+ }),
611
+ Layer.succeed(HostProcess.Platform, mode === "darwin" ? "darwin" : "linux"),
612
+ FetchHttpClient.layer,
613
+ ),
614
+ ),
615
+ );
616
+ return Effect.gen(function* () {
617
+ const scanner = yield* ownedScanner();
618
+ expect((yield* scanner.scan()).map(({ port }) => port)).toEqual(
619
+ mode === "fd-budget-no-lsof" ? [8765] : fallback ? [LSOF_TEST_PORT] : [],
620
+ );
621
+ if (mode === "fd-budget-no-lsof") {
622
+ for (let i = 0; i < 3; i += 1) {
623
+ expect((yield* scanner.scan()).map(({ port }) => port)).toEqual([8765]);
624
+ }
625
+ expect(procWalks).toBe(4);
626
+ }
627
+ expect(lsofSpawns).toBe(fallback ? 1 : 0);
628
+ }).pipe(Effect.provide(layer));
629
+});
630
+
631
effectIt.effect("ignores a partial Windows netstat snapshot when one query exits nonzero", () => {
632
let ipv6Fails = true;
633
const requests: string[] = [];
apps/server/src/preview/PortScanner.ts
+42
-19
@@ -311,7 +311,7 @@ const PROC_TCP_LISTEN = "0A";
311
/** Each 32-bit word of the hex address is little-endian. */
312
const procLocalAddress = (hex: string): boolean => {
313
if (hex.length === 8) {
314
- return hex === "00000000" || hex.endsWith("7F");
314
+ return hex === "00000000" || hex === "0100007F";
315
}
316
if (hex.length !== 32) return false;
317
if (hex === "0".repeat(32) || hex === `${"0".repeat(24)}01000000`) return true;
@@ -543,11 +543,9 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
543
inodes: ReadonlySet<string>,
544
) {
545
const owners = new Map<string, { pid: number; processName: string | null }>();
546
- const pids = (yield* fileSystem
547
- .readDirectory("/proc")
548
- .pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []))).filter((entry) =>
549
- /^\d+$/.test(entry),
550
- );
546
+ const entries = yield* fileSystem.readDirectory("/proc").pipe(Effect.option);
547
+ if (Option.isNone(entries)) return null;
548
+ const pids = entries.value.filter((entry) => /^\d+$/.test(entry));
549
let budget = PROC_FD_WALK_LIMIT;
550
for (const pidText of pids) {
551
if (owners.size === inodes.size || budget <= 0) break;
@@ -571,7 +569,8 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
569
);
570
for (const inode of matched) owners.set(inode, { pid: Number(pidText), processName });
571
}
574
- return owners;
572
+ const lsofMissing = yield* Ref.get(lsofMissingRef);
573
+ return budget <= 0 && owners.size < inodes.size && !lsofMissing ? null : owners;
574
});
575
576
const scanProcListeners = Effect.fn("PortDiscovery.scanProcListeners")(function* (
@@ -579,12 +578,27 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
578
) {
579
if (hostPlatform !== "linux") return null;
580
const tables = yield* Effect.forEach(["/proc/net/tcp", "/proc/net/tcp6"], (path) =>
582
- fileSystem.readFileString(path).pipe(Effect.option),
581
+ fileSystem.readFileString(path).pipe(
582
+ // Kernels without IPv6 omit tcp6; a denied table can hide listeners on a shared port.
583
+ Effect.catchIf(
584
+ (error) => error.reason._tag === "NotFound",
585
+ () => Effect.succeed(null),
586
+ ),
587
+ Effect.option,
588
+ ),
589
);
584
- if (tables.every(Option.isNone)) return null;
585
- const listeners = tables.flatMap((table) =>
586
- Option.isSome(table) ? parseProcNetTcp(table.value) : [],
590
+ if (tables.some(Option.isNone)) return null;
591
+ const readable = tables.flatMap((table) =>
592
+ Option.isSome(table) && table.value !== null ? [table.value] : [],
593
);
594
+ if (
595
+ readable.length === 0 ||
596
+ readable.some(
597
+ (table) => !/^\s*sl\s+local_address\s+(?:rem|remote)_address\s+st\s/.test(table),
598
+ )
599
+ )
600
+ return null;
601
+ const listeners = readable.flatMap(parseProcNetTcp);
602
const known = yield* Ref.get(socketOwnersRef);
603
const knownMisses = yield* Ref.get(socketOwnerMissesRef);
604
const inodes = new Set(listeners.map((listener) => listener.inode));
@@ -595,6 +609,7 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
609
),
610
);
611
const found = unseen.size === 0 ? new Map() : yield* findSocketOwners(unseen);
612
+ if (found === null) return null;
613
const owners = new Map([...known, ...found].filter(([inode]) => inodes.has(inode)));
614
const misses = new Map([...knownMisses].filter(([inode]) => inodes.has(inode)));
615
for (const inode of unseen) {
@@ -825,11 +840,10 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
840
yield* probeWebServers(listeners ?? [], configuredUrls),
841
);
842
}
843
+ const fromProc = yield* scanProcListeners(resolveOwners);
844
+ if (fromProc !== null) return yield* probeWebServers(fromProc, configuredUrls);
845
const recoverLsofProbeFailure = recoverProcessProbeFailure("lsof");
829
- if (yield* Ref.get(lsofMissingRef)) {
830
- const fromProc = yield* scanProcListeners(resolveOwners);
831
- return yield* probeWebServers(fromProc ?? [], configuredUrls);
832
- }
846
+ if (yield* Ref.get(lsofMissingRef)) return yield* probeWebServers([], configuredUrls);
847
const lsofResult = yield* processRunner
848
.run({
849
command: "lsof",
@@ -856,8 +870,11 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
870
}),
871
);
872
if (lsofResult !== null) return yield* probeWebServers(lsofResult, configuredUrls);
859
- const fromProc = yield* scanProcListeners(resolveOwners);
860
- return yield* probeWebServers(fromProc ?? [], configuredUrls);
873
+ // A missing lsof makes a bounded, partial /proc owner walk the only available discovery.
874
+ const withoutLsof = (yield* Ref.get(lsofMissingRef))
875
+ ? yield* scanProcListeners(resolveOwners)
876
+ : null;
877
+ return yield* probeWebServers(withoutLsof ?? [], configuredUrls);
878
});
879
880
const scanSnapshot = (configuredUrls: ReadonlyArray<string>) =>
@@ -922,8 +939,14 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
939
return [retainCount === 0, { ...state, retainCount }];
940
});
941
if (!idle) return;
925
- const threads = yield* Ref.getAndSet(agentThreadsRef, new Map());
926
- if ([...threads.values()].some((threadId) => threadId !== null)) agentScanPending = true;
942
+ yield* scanSemaphore.withPermits(1)(
943
+ Effect.gen(function* () {
944
+ yield* Ref.set(socketOwnersRef, new Map());
945
+ yield* Ref.set(socketOwnerMissesRef, new Map());
946
+ const threads = yield* Ref.getAndSet(agentThreadsRef, new Map());
947
+ if ([...threads.values()].some((threadId) => threadId !== null)) agentScanPending = true;
948
+ }),
949
+ );
950
});
951
952
const retain: PortDiscovery["Service"]["retain"] = Effect.acquireRelease(