Skip to content

Commit 0b9c77e

Browse files
cliffhallclaude
andcommitted
fix: relaunch on a lost port at either step; end the tree on Windows
Copilot round 2 on #4920: - The smoke's lost-port check moves to the outer catch, so it also covers the case where the listen probe reached the other process and the MCP connection is what failed. Free ports are probed on the wildcard address, as the servers bind them. - gate-lease ends the child's process tree with `taskkill /T` on Windows, where there are no process groups. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent c41cf22 commit 0b9c77e

2 files changed

Lines changed: 43 additions & 15 deletions

File tree

‎scripts/gate-lease.mjs‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,28 @@ export function spawnSpec(command, args, platform = process.platform) {
464464

465465
/**
466466
* Send `signal` to the child's whole process group (it was spawned as a group
467-
* leader), falling back to the child alone where groups are unavailable or the
468-
* group is already gone.
467+
* leader); on Windows, end its process tree with `taskkill`. Falls back to the
468+
* child alone where neither works or the group is already gone.
469469
*/
470470
function signalTree(child, signal) {
471+
if (process.platform === "win32") {
472+
// No process groups, and `child.kill` ends only the `cmd.exe` the gate was
473+
// started through: the builds and tests under it would run on after the
474+
// lease is released, beside the next gate. `taskkill /T` takes the tree
475+
// (as `killTree` in scripts/skill-eval.mjs does); `/F` because Windows has
476+
// no graceful signal to offer a console tree anyway.
477+
try {
478+
const killer = spawn(
479+
"taskkill",
480+
["/pid", String(child.pid), "/T", "/F"],
481+
{ stdio: "ignore" },
482+
);
483+
killer.on("error", () => child.kill(signal));
484+
return;
485+
} catch {
486+
// Fall through to the child alone.
487+
}
488+
}
471489
try {
472490
if (process.platform !== "win32") {
473491
process.kill(-child.pid, signal);

‎scripts/smoke-servers.mjs‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
* on a port the OS reported free a moment earlier, not the server's default
4040
* 3001, so the smoke does not collide with a server the developer has running.
4141
* That port is not reserved between the probe and the server's own `listen`,
42-
* so a launch that loses it to another process is relaunched on a fresh one.
42+
* so a launch whose server reports the port taken is relaunched on a fresh
43+
* one. Streamable HTTP reports it; HTTP+SSE does not yet (#4923: it prints
44+
* "Server is running" either way), so there a lost port is a failed smoke.
4345
*
4446
* One server failing does not stop the rest: the run reports every target's
4547
* verdict and exits non-zero if any failed, so one run shows the whole picture.
@@ -295,12 +297,17 @@ async function exercise(client, server, ctx) {
295297
);
296298
}
297299

298-
/** A port the OS says is free right now. */
300+
/**
301+
* A port the OS says is free right now. Probed on the wildcard address, the
302+
* way the servers bind it (`app.listen(PORT)`): a port held on one interface
303+
* only can be bound again on the wildcard without an error, and the client
304+
* would then reach the holder, not the server.
305+
*/
299306
function freePort() {
300307
return new Promise((resolve, reject) => {
301308
const probe = createNetServer();
302309
probe.once("error", reject);
303-
probe.listen(0, "127.0.0.1", () => {
310+
probe.listen(0, () => {
304311
const { port } = probe.address();
305312
probe.close(() => resolve(port));
306313
});
@@ -395,8 +402,12 @@ async function smokeOne(server, transport, ctx) {
395402

396403
// The probe port is free when probed, not reserved: another process can take
397404
// it before the server binds. Only that one failure is relaunched, on a fresh
398-
// port; anything else the server says on its way down is the result.
405+
// port; anything else the server says on its way down is the result. The
406+
// check sits in the outer catch because the loss can surface at either step:
407+
// the listen probe can reach the OTHER process on that port before our child
408+
// has exited, and then it is the MCP connection that fails.
399409
for (let attempt = 1; ; attempt += 1) {
410+
const httpClient = new Client({ name: "boot-smoke", version: "0.0.0" });
400411
const port = await freePort();
401412
let output = "";
402413
const child = spawn(spec.command, spec.args, {
@@ -412,12 +423,7 @@ async function smokeOne(server, transport, ctx) {
412423
child.once("error", reject);
413424
child.once("spawn", resolve);
414425
});
415-
try {
416-
await waitForListen(port, child, tail);
417-
} catch (err) {
418-
if (attempt < PORT_ATTEMPTS && isPortTaken(output)) continue;
419-
throw err;
420-
}
426+
await waitForListen(port, child, tail);
421427
const url = new URL(
422428
`http://127.0.0.1:${port}${TRANSPORT_ENDPOINT[transport]}`,
423429
);
@@ -426,13 +432,17 @@ async function smokeOne(server, transport, ctx) {
426432
? new SSEClientTransport(url)
427433
: new StreamableHTTPClientTransport(url);
428434
try {
429-
await client.connect(clientTransport, options);
430-
await exercise(client, server, ctx);
435+
await httpClient.connect(clientTransport, options);
436+
await exercise(httpClient, server, ctx);
431437
} finally {
432-
await client.close().catch(() => {});
438+
await httpClient.close().catch(() => {});
433439
}
434440
return;
435441
} catch (err) {
442+
// Give a child that is on its way down a moment to say why.
443+
if (child.exitCode === null && child.signalCode === null)
444+
await delay(200);
445+
if (attempt < PORT_ATTEMPTS && isPortTaken(output)) continue;
436446
throw new Error(
437447
`${err?.message ?? err}${tail() ? `\n--- server output ---\n${tail()}` : ""}`,
438448
{ cause: err },

0 commit comments

Comments
 (0)