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.
This commit is contained in:
Aamer Akhter committed 2026-11-15 15:09:20 -05:00
1 parent e99b42c387
commit 0e913eedf2
3 files changed
+96 -1

No files matched your search

+22 -1
View File
@@ -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() {