mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-20 04:59:34 +01:00
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:
1 parent
e99b42c387
commit
0e913eedf2
3 files changed
+96
-1
No files matched your search
@@ -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<number>((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<void>((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<void>((resolve) => server.close(() => resolve()));
|
||||
}
|
||||
const closed = probe('::1', port);
|
||||
expect(closed.status).toBe(1);
|
||||
expect(closed.ms).toBeLessThan(1000);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user