From 0e913eedf24a698d3e92773ebe6ae27138640508 Mon Sep 17 00:00:00 2001 From: Aamer Akhter Date: Sun, 15 Nov 2026 15:09:20 -0500 Subject: [PATCH] fix(docker): bound Start-Codeman.sh's port probe to about a second port_in_use() connected with bash's /dev/tcp and waited for the answer. A listening port answers at once and a closed one is normally refused at once, but on WSL a connect to a closed 127.0.0.1 port is dropped and only fails at the kernel's connect timeout, minutes later (measured: still waiting after 10 s; ::1 is refused at once). The first-run port search tries up to 100 ports from 3000 and the start path checks the chosen port again, so a first run on WSL stalled. Each connect now runs in a background subshell that port_answers() waits on for at most 20 x 50 ms, then kills; no answer in time reads as free. Bash 3.2 clean: no `timeout` (macOS ships none), no `wait -n`. Test: a python3 listener with a full accept queue (Linux drops further SYNs, the same no-answer a WSL closed port gives) must be given up on within 3 s; a listening port and a closed one still answer at once. With the old unbounded connect the first test fails at its 10 s cap. --- .changeset/start-codeman-port-probe.md | 5 ++ docker/Start-Codeman.sh | 23 ++++++++- test/docker-start-setup.test.ts | 69 ++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 .changeset/start-codeman-port-probe.md diff --git a/.changeset/start-codeman-port-probe.md b/.changeset/start-codeman-port-probe.md new file mode 100644 index 00000000..c86dcd36 --- /dev/null +++ b/.changeset/start-codeman-port-probe.md @@ -0,0 +1,5 @@ +--- +'aicodeman': patch +--- + +`docker/Start-Codeman.sh` no longer hangs on WSL while it looks for a free port. On WSL a connect to a closed 127.0.0.1 port is dropped rather than refused and only fails at the kernel's connect timeout, so the first-run port search (up to 100 ports) and the check before `docker compose up` could stall for many minutes. Each port check now gives up after about a second and reads no answer as free. diff --git a/docker/Start-Codeman.sh b/docker/Start-Codeman.sh index 9f94c058..68f573b4 100644 --- a/docker/Start-Codeman.sh +++ b/docker/Start-Codeman.sh @@ -156,10 +156,31 @@ ask() { fi } +# True when host $1 accepts a connection on port $2 within about a second. +# A listening port answers at once and a closed one is normally refused at +# once, but on WSL a connect to a closed 127.0.0.1 port is dropped and only +# fails at the kernel's connect timeout, minutes later. No answer in time +# reads as free. Bash 3.2 clean: no `timeout` (macOS ships none), no `wait -n`. +port_answers() { + local pid tries=0 + (exec 3<>"/dev/tcp/$1/$2") 2>/dev/null & + pid=$! + while kill -0 "$pid" 2>/dev/null; do + if ((tries >= 20)); then + kill "$pid" 2>/dev/null + wait "$pid" 2>/dev/null + return 1 + fi + tries=$((tries + 1)) + sleep 0.05 + done + wait "$pid" +} + # True when something on this host already accepts connections on the port. # bash's /dev/tcp needs no extra tool on Linux or macOS. port_in_use() { - (exec 3<>"/dev/tcp/127.0.0.1/$1") 2>/dev/null || (exec 3<>"/dev/tcp/::1/$1") 2>/dev/null + port_answers 127.0.0.1 "$1" || port_answers ::1 "$1" } first_free_port() { diff --git a/test/docker-start-setup.test.ts b/test/docker-start-setup.test.ts index 045a6de8..ecfee7b9 100644 --- a/test/docker-start-setup.test.ts +++ b/test/docker-start-setup.test.ts @@ -639,3 +639,72 @@ describe('version_older_than', () => { expect(out).toBe(older ? 'yes' : 'no'); }); }); + +/** + * A listener whose accept queue is full, so the kernel drops further SYNs and a + * connect to it gets no answer, the same thing a WSL host does for a CLOSED + * 127.0.0.1 port. Held by a python3 child, since Node accepts every connection + * as it arrives and so can never leave its own queue full. + */ +const HAS_PYTHON = spawnSync('python3', ['-c', 'pass']).status === 0; +const SILENT_LISTENER = [ + 'import socket, sys, time', + 's = socket.socket(); s.bind(("127.0.0.1", 0)); s.listen(0)', + 'port = s.getsockname()[1]; held = []', + 'for _ in range(3):', + ' c = socket.socket(); c.setblocking(False)', + ' try: c.connect(("127.0.0.1", port))', + ' except BlockingIOError: pass', + ' held.append(c)', + 'time.sleep(0.3); print(port, flush=True); time.sleep(60)', +].join('\n'); + +describe.skipIf(process.platform !== 'linux' || !HAS_PYTHON)('port_answers', () => { + /** Runs the script's own port_answers on host/port, returning its status and how long it took. */ + function probe(host: string, port: number): { status: number | null; ms: number } { + const script = [ + 'set -euo pipefail', + `eval "$(sed -n '/^port_answers() {/,/^}/p' "$1")"`, + 'if port_answers "$2" "$3"; then exit 0; else exit 1; fi', + ].join('\n'); + const started = Date.now(); + const res = spawnSync('bash', ['-c', script, '_', join(ROOT, 'docker/Start-Codeman.sh'), host, String(port)], { + encoding: 'utf-8', + timeout: 10_000, + }); + return { status: res.status, ms: Date.now() - started }; + } + + it('gives up on a connect that gets no answer within about a second, and reads it as free', async () => { + const { spawn } = await import('node:child_process'); + const child = spawn('python3', ['-I', '-c', SILENT_LISTENER], { stdio: ['ignore', 'pipe', 'ignore'] }); + try { + const port = await new Promise((resolve, reject) => { + child.stdout!.once('data', (chunk: Buffer) => resolve(Number(String(chunk).trim()))); + child.once('exit', () => reject(new Error('listener exited'))); + }); + const { status, ms } = probe('127.0.0.1', port); + expect(status).toBe(1); + expect(ms).toBeLessThan(3000); + } finally { + child.kill(); + } + }); + + it('still answers at once for a listening port and a closed one', async () => { + const net = await import('node:net'); + const server = net.createServer(); + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); + const port = (server.address() as { port: number }).port; + try { + const open = probe('127.0.0.1', port); + expect(open.status).toBe(0); + expect(open.ms).toBeLessThan(1000); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + const closed = probe('::1', port); + expect(closed.status).toBe(1); + expect(closed.ms).toBeLessThan(1000); + }); +});