The best way to control Claude Code, Codex and more from any computer or phone
perf(server): skip idle port discovery on Linux and macOS (#20975)
Yukun Shan committed
Oct 19, 2026 at 03:18 UTC
3755b4970fb591355e2a9ca556b0cee18ff36533
2 files changed
+73
-6
apps/server/src/preview/PortScanner.test.ts
+70
-3
@@ -655,7 +655,7 @@ effectIt.effect("does not swallow process probe defects", () =>
655
const defect = new Error("unexpected process probe defect");
656
const layer = layerProbeFailure(() => Effect.die(defect));
657
658
- const exit = yield* Effect.flatMap(PortScanner.PortDiscovery, (scanner) => scanner.scan()).pipe(
658
+ const exit = yield* Effect.flatMap(ownedScanner(), (scanner) => scanner.scan()).pipe(
659
Effect.provide(layer),
660
Effect.exit,
661
);
@@ -760,7 +760,7 @@ effectIt.effect("keeps spawning lsof after a spawn failure that is not a missing
760
);
761
});
762
return Effect.gen(function* () {
763
- const scanner = yield* PortScanner.PortDiscovery;
763
+ const scanner = yield* ownedScanner();
764
yield* scanner.scan();
765
yield* scanner.scan();
766
expect(lsofSpawns).toBe(2);
@@ -771,7 +771,7 @@ effectIt.effect("does not swallow process probe interruption", () =>
771
Effect.gen(function* () {
772
const layer = layerProbeFailure(() => Effect.interrupt);
773
774
- const exit = yield* Effect.flatMap(PortScanner.PortDiscovery, (scanner) => scanner.scan()).pipe(
774
+ const exit = yield* Effect.flatMap(ownedScanner(), (scanner) => scanner.scan()).pipe(
775
Effect.provide(layer),
776
Effect.exit,
777
);
@@ -783,6 +783,73 @@ effectIt.effect("does not swallow process probe interruption", () =>
783
}),
784
);
785
786
+effectIt.effect.each(["linux", "darwin"] as const)(
787
+ "skips idle listener scans on %s and resumes when candidates exist",
788
+ (platform) => {
789
+ let processCalls = 0;
790
+ const layer = PortScanner.layer.pipe(
791
+ Layer.provide(
792
+ Layer.mergeAll(
793
+ layerNoProc,
794
+ Layer.succeed(HostProcess.Platform, platform),
795
+ Layer.succeed(ProcessRunner.ProcessRunner, {
796
+ run: () => {
797
+ processCalls += 1;
798
+ return Effect.succeed({
799
+ stdout: `p1234\ncnode\nn*:${LSOF_TEST_PORT}\n`,
800
+ stderr: "",
801
+ code: ChildProcessSpawner.ExitCode(0),
802
+ timedOut: false,
803
+ stdoutTruncated: false,
804
+ stderrTruncated: false,
805
+ stdoutInvalidUtf8: false,
806
+ stderrInvalidUtf8: false,
807
+ });
808
+ },
809
+ }),
810
+ FetchHttpClient.layer.pipe(
811
+ Layer.provide(
812
+ Layer.succeed(FetchHttpClient.Fetch, () =>
813
+ Promise.resolve(
814
+ new Response("hello", { headers: { "content-type": "text/html" } }),
815
+ ),
816
+ ),
817
+ ),
818
+ ),
819
+ ),
820
+ ),
821
+ );
822
+ return Effect.gen(function* () {
823
+ const scanner = yield* PortScanner.PortDiscovery;
824
+ yield* scanner.retain;
825
+ yield* TestClock.adjust("9 seconds");
826
+ expect(yield* scanner.scan()).toEqual([]);
827
+ expect(processCalls).toBe(0);
828
+
829
+ yield* scanner.registerTerminalProcesses({
830
+ threadId: "scanner-thread",
831
+ terminalId: "scanner-terminal",
832
+ processIds: [1234],
833
+ });
834
+ expect(yield* scanner.scan()).toMatchObject([{ port: LSOF_TEST_PORT, pid: 1234 }]);
835
+ expect(processCalls).toBe(1);
836
+
837
+ yield* scanner.unregisterTerminal({
838
+ threadId: "scanner-thread",
839
+ terminalId: "scanner-terminal",
840
+ });
841
+ yield* TestClock.adjust("9 seconds");
842
+ expect(yield* scanner.scan()).toEqual([]);
843
+ expect(processCalls).toBe(1);
844
+
845
+ expect(yield* scanner.scan([`http://localhost:${LSOF_TEST_PORT}/docs`])).toMatchObject([
846
+ { port: LSOF_TEST_PORT, url: `http://localhost:${LSOF_TEST_PORT}/docs` },
847
+ ]);
848
+ expect(processCalls).toBe(2);
849
+ }).pipe(Effect.provide(layer));
850
+ },
851
+);
852
+
853
effectIt.effect(
854
"parses Windows netstat listeners, names them via tasklist with retry, and skips spawns when idle",
855
() => {
apps/server/src/preview/PortScanner.ts
+3
-3
@@ -692,10 +692,10 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
692
terminalByProcessId.set(processId, registration.owner);
693
}
694
}
695
+ if (terminalByProcessId.size === 0 && configuredUrls.length === 0) {
696
+ return { discovered: [], configured: new Map() } satisfies WebProbeSnapshot;
697
+ }
698
if (hostPlatform === "win32") {
696
- if (terminalByProcessId.size === 0 && configuredUrls.length === 0) {
697
- return { discovered: [], configured: new Map() } satisfies WebProbeSnapshot;
698
- }
699
const recoverWindowsProbeFailure = recoverProcessProbeFailure("windows-listeners");
700
const runNetstat = (protocol: "tcp" | "tcpv6") =>
701
processRunner.run({