Skip to content

Commit 9e4235f

Browse files
t3dotggclaude
andcommitted
fix(server): a failed listener read keeps the agent scan pending
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent abf1888 commit 9e4235f

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

‎apps/server/src/preview/PortScanner.test.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ effectIt.effect.each(["linux", "darwin"] as const)(
10021002
const agentListener = `p4242\ncnode\nn127.0.0.1:${agentPort}\n`;
10031003
const otherListener = `p5000\ncpostgres\nn127.0.0.1:${otherPort}\n`;
10041004
let lsofStdout = `${agentListener}${otherListener}`;
1005+
let lsofFails = false;
10051006
let lsofCalls = 0;
10061007
let environmentReads = 0;
10071008
const requests: string[] = [];
@@ -1025,7 +1026,16 @@ effectIt.effect.each(["linux", "darwin"] as const)(
10251026
run: (input) => {
10261027
if (input.command === "lsof") {
10271028
lsofCalls += 1;
1028-
return Effect.succeed(processOutput(lsofStdout));
1029+
return lsofFails
1030+
? Effect.fail(
1031+
new ProcessRunner.ProcessTimeoutError({
1032+
command: input.command,
1033+
argumentCount: input.args.length,
1034+
cwd: input.cwd,
1035+
timeoutMs: 5_000,
1036+
}),
1037+
)
1038+
: Effect.succeed(processOutput(lsofStdout));
10291039
}
10301040
// A direct child of this process, so stop refuses it without signaling anything.
10311041
if (input.args[0] === "-A") {
@@ -1060,8 +1070,12 @@ effectIt.effect.each(["linux", "darwin"] as const)(
10601070
expect(yield* scanner.scan()).toEqual([]);
10611071
expect(lsofCalls).toBe(0);
10621072

1063-
// The agent exits before any client looks; its server is still found.
1073+
// The agent exits before any client looks, and the first read fails; its server is still
1074+
// found.
10641075
scanner.trackAgent()();
1076+
lsofFails = true;
1077+
expect(yield* scanner.scan()).toEqual([]);
1078+
lsofFails = false;
10651079
expect(yield* scanner.scan()).toMatchObject([
10661080
{ port: agentPort, pid: 4242, terminal: null, agent: { threadId: "thread-agent" } },
10671081
]);

‎apps/server/src/preview/PortScanner.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,8 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
568568
terminalOwners: ReadonlyMap<number, ServerOwner>,
569569
pids: ReadonlyArray<number>,
570570
) {
571+
// Runs only once a listener snapshot exists, so a failed read leaves the scan pending.
572+
agentScanPending = false;
571573
const threads = yield* readAgentThreads(pids.filter((pid) => !terminalOwners.has(pid)));
572574
const owners = new Map(terminalOwners);
573575
for (const [pid, threadId] of threads) {
@@ -886,7 +888,6 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
886888
(runningAgents > 0 ||
887889
agentScanPending ||
888890
[...(yield* Ref.get(agentThreadsRef)).values()].some((threadId) => threadId !== null));
889-
if (agentsPossible) agentScanPending = false;
890891
if (terminalOwners.size === 0 && configuredUrls.length === 0 && !agentsPossible) {
891892
return { discovered: [], configured: new Map() } satisfies WebProbeSnapshot;
892893
}

0 commit comments

Comments
 (0)