@@ -2,7 +2,7 @@ import { beforeEach, describe, expect, spyOn, test } from "bun:test"; |
2 | 2 | import { createHash } from "node:crypto"; |
3 | 3 | import { spawnSync } from "node:child_process"; |
4 | 4 | import { existsSync, mkdtempSync, readdirSync, readFileSync, writeFileSync, mkdirSync, unlinkSync } from "node:fs"; |
5 | | -import { createServer } from "node:net"; |
| 5 | +import { createConnection, createServer } from "node:net"; |
6 | 6 | import type { AddressInfo } from "node:net"; |
7 | 7 | import { tmpdir } from "node:os"; |
8 | 8 | import { dirname, join } from "node:path"; |
@@ -879,6 +879,24 @@ describe("status reports stale process records end to end", () => { |
879 | 879 | await new Promise<void>(resolve => { probe.close(() => resolve()); }); |
880 | 880 | return port; |
881 | 881 | } |
| 882 | + |
| 883 | + /** |
| 884 | + * Prove the endpoint refuses right now. |
| 885 | + * |
| 886 | + * `allocateFreePort` releases the port it reports, and on a sharded runner every other |
| 887 | + * test binding an ephemeral port is a candidate to take it. A fixture that assumes a |
| 888 | + * released port is still refusing asserts against whatever happened to bind it. |
| 889 | + */ |
| 890 | + async function refusesConnection(port: number): Promise<boolean> { |
| 891 | + return await new Promise<boolean>(resolve => { |
| 892 | + const socket = createConnection({ port, host: "127.0.0.1" }); |
| 893 | + const settle = (refused: boolean): void => { socket.destroy(); resolve(refused); }; |
| 894 | + socket.setTimeout(1_000); |
| 895 | + socket.once("connect", () => settle(false)); |
| 896 | + socket.once("timeout", () => settle(false)); |
| 897 | + socket.once("error", () => settle(true)); |
| 898 | + }); |
| 899 | + } |
882 | 900 | let freePort: number; |
883 | 901 | beforeEach(async () => { freePort = await allocateFreePort(); }); |
884 | 902 |
|
@@ -950,17 +968,27 @@ describe("status reports stale process records end to end", () => { |
950 | 968 | await new Promise<void>(resolve => { occupied.listen(0, "127.0.0.1", () => resolve()); }); |
951 | 969 | const occupiedPort = (occupied.address() as AddressInfo).port; |
952 | 970 | try { |
953 | | - // Allocate after the listener is bound: it can reuse the port released by |
954 | | - // beforeEach, so that earlier number no longer proves a refused endpoint. |
955 | | - const recordedPort = await allocateFreePort(); |
956 | | - expect(recordedPort).not.toBe(occupiedPort); |
957 | 971 | const pid = findDeadPid(); |
958 | 972 | writeFileSync(join(home, "config.json"), JSON.stringify({ port: occupiedPort, codexAutoStart: false }), "utf8"); |
959 | 973 | writeFileSync(join(home, "ocx.pid"), String(pid), "utf8"); |
960 | | - writeFileSync(join(home, "runtime-port.json"), JSON.stringify({ pid, port: recordedPort, hostname: "127.0.0.1" }), "utf8"); |
961 | 974 |
|
962 | | - const parsed = JSON.parse(runStatusJson(home).stdout) as { proxy?: { staleProcessState?: unknown } }; |
963 | | - expect(parsed.proxy?.staleProcessState).toBe(true); |
| 975 | + // The recorded port has to refuse for this to discriminate, and `allocateFreePort` |
| 976 | + // hands back a port it has already released. Confirm refusal immediately before and |
| 977 | + // immediately after the probe, and re-allocate when something took it in between, so |
| 978 | + // a stolen port retries instead of failing an assertion it never exercised. |
| 979 | + let parsed: { proxy?: { staleProcessState?: unknown } } | undefined; |
| 980 | + for (let attempt = 0; attempt < 5 && parsed === undefined; attempt++) { |
| 981 | + const recordedPort = await allocateFreePort(); |
| 982 | + if (recordedPort === occupiedPort) continue; |
| 983 | + if (!await refusesConnection(recordedPort)) continue; |
| 984 | + writeFileSync(join(home, "runtime-port.json"), JSON.stringify({ pid, port: recordedPort, hostname: "127.0.0.1" }), "utf8"); |
| 985 | + const observed = JSON.parse(runStatusJson(home).stdout) as { proxy?: { staleProcessState?: unknown } }; |
| 986 | + if (!await refusesConnection(recordedPort)) continue; |
| 987 | + parsed = observed; |
| 988 | + } |
| 989 | + |
| 990 | + expect(parsed, "no allocated port stayed refused across the status probe").toBeDefined(); |
| 991 | + expect(parsed?.proxy?.staleProcessState).toBe(true); |
964 | 992 | } finally { |
965 | 993 | await new Promise<void>(resolve => { occupied.close(() => resolve()); }); |
966 | 994 | removeTreeWithRetry(home); |
|