Skip to content

Commit 920f825

Browse files
author
RioPlay
committed
fix(server): clear Windows port-probe in-flight flag on interrupt
Claiming the single-flight probe then attaching Effect.ensuring later left a window where interruption stuck windowsListenerProbeInFlight true forever. Bracket claim/use/release with acquireUseRelease and cover recovery after interrupt.
1 parent c74fb6b commit 920f825

2 files changed

Lines changed: 92 additions & 25 deletions

File tree

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,62 @@ effectIt("does not swallow process probe interruption", () =>
316316
}
317317
}),
318318
);
319+
320+
effectIt("clears Windows listener probe in-flight flag after interruption", () =>
321+
Effect.gen(function* () {
322+
let probeRuns = 0;
323+
const layer = PortScanner.layer.pipe(
324+
Layer.provide(
325+
Layer.mergeAll(
326+
Layer.succeed(ProcessRunner.ProcessRunner, {
327+
run: () => {
328+
probeRuns += 1;
329+
if (probeRuns === 1) {
330+
return Effect.interrupt;
331+
}
332+
return Effect.succeed({
333+
stdout: "127.0.0.1|5173|4242|node\n",
334+
stderr: "",
335+
code: 0,
336+
timedOut: false,
337+
stdoutTruncated: false,
338+
stderrTruncated: false,
339+
stdoutInvalidUtf8: false,
340+
stderrInvalidUtf8: false,
341+
});
342+
},
343+
}),
344+
Layer.succeed(Net.NetService, {
345+
canListenOnHost: () => Effect.succeed(true),
346+
isPortAvailableOnLoopback: () => Effect.succeed(true),
347+
reserveLoopbackPort: () => Effect.succeed(40_000),
348+
findAvailablePort: (preferred) => Effect.succeed(preferred),
349+
}),
350+
Layer.succeed(HostProcessPlatform, "win32"),
351+
),
352+
),
353+
);
354+
355+
yield* Effect.gen(function* () {
356+
const scanner = yield* PortScanner.PortDiscovery;
357+
const first = yield* scanner.scan().pipe(Effect.exit);
358+
expect(Exit.isFailure(first)).toBe(true);
359+
if (Exit.isFailure(first)) {
360+
expect(Cause.hasInterruptsOnly(first.cause)).toBe(true);
361+
}
362+
// Release must clear inFlight; otherwise every later scan would skip.
363+
const second = yield* scanner.scan();
364+
expect(probeRuns).toBe(2);
365+
expect(second).toEqual([
366+
{
367+
host: "localhost",
368+
port: 5173,
369+
url: "http://localhost:5173",
370+
processName: "node",
371+
pid: 4242,
372+
terminal: null,
373+
},
374+
]);
375+
}).pipe(Effect.provide(layer), Effect.scoped);
376+
}),
377+
);

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

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -287,32 +287,40 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
287287
if (hostPlatform === "win32") {
288288
// Wall-clock cooldown + single-flight so retain/scan/poll cannot burn a
289289
// tick budget or spawn overlapping PowerShell/WMI probes (#5900).
290-
const gate = yield* claimWindowsListenerProbe;
291-
if (gate === "skip") {
292-
return yield* probeCommonPorts();
293-
}
294-
290+
// acquireUseRelease (not claim + later ensuring): if the fiber is
291+
// interrupted after claim and before ensuring is installed, inFlight
292+
// would stick true and every later scan would skip forever.
295293
const recoverWindowsProbeFailure = recoverProcessProbeFailure("windows-listeners");
296-
const listeners = yield* processRunner
297-
.run({
298-
command: "powershell.exe",
299-
args: ["-NoProfile", "-NonInteractive", "-Command", WINDOWS_LISTENER_COMMAND],
300-
timeout: Duration.millis(WINDOWS_LISTENER_TIMEOUT_MS),
301-
maxOutputBytes: 1024 * 1024,
302-
outputMode: "truncate",
303-
})
304-
.pipe(
305-
Effect.map((result) => parseWindowsListenerOutput(result.stdout, terminalByProcessId)),
306-
Effect.catchTags({
307-
ProcessSpawnError: recoverWindowsProbeFailure,
308-
ProcessStdinError: recoverWindowsProbeFailure,
309-
ProcessOutputLimitError: recoverWindowsProbeFailure,
310-
ProcessReadError: recoverWindowsProbeFailure,
311-
ProcessTimeoutError: (error) =>
312-
armWindowsListenerCooldown.pipe(Effect.zipRight(recoverWindowsProbeFailure(error))),
313-
}),
314-
Effect.ensuring(releaseWindowsListenerProbe),
315-
);
294+
const listeners = yield* Effect.acquireUseRelease(
295+
claimWindowsListenerProbe,
296+
(gate) => {
297+
if (gate === "skip") {
298+
return Effect.succeed(null);
299+
}
300+
return processRunner
301+
.run({
302+
command: "powershell.exe",
303+
args: ["-NoProfile", "-NonInteractive", "-Command", WINDOWS_LISTENER_COMMAND],
304+
timeout: Duration.millis(WINDOWS_LISTENER_TIMEOUT_MS),
305+
maxOutputBytes: 1024 * 1024,
306+
outputMode: "truncate",
307+
})
308+
.pipe(
309+
Effect.map((result) => parseWindowsListenerOutput(result.stdout, terminalByProcessId)),
310+
Effect.catchTags({
311+
ProcessSpawnError: recoverWindowsProbeFailure,
312+
ProcessStdinError: recoverWindowsProbeFailure,
313+
ProcessOutputLimitError: recoverWindowsProbeFailure,
314+
ProcessReadError: recoverWindowsProbeFailure,
315+
ProcessTimeoutError: (error) =>
316+
armWindowsListenerCooldown.pipe(
317+
Effect.zipRight(recoverWindowsProbeFailure(error)),
318+
),
319+
}),
320+
);
321+
},
322+
(gate) => (gate === "run" ? releaseWindowsListenerProbe : Effect.void),
323+
);
316324
if (listeners !== null) return listeners;
317325
return yield* probeCommonPorts();
318326
}

0 commit comments

Comments
 (0)