Skip to content

Commit 5893f30

Browse files
test(federated-css): harden Next start against EADDRINUSE\n\n- Add ensurePortFree() using lsof/fuser loop before next start\n- Log and enforce port clearance (esp. 8084) to avoid flakiness
1 parent cc460d3 commit 5893f30

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

federated-css/scripts/start-all.cjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,31 @@ const { aggressiveKillPort, aggressiveKillPorts } = require('./aggressive-port-c
77
const WAIT_TIMEOUT = 480000;
88
const isReadyStatus = status => status >= 200 && status < 400;
99
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
10+
const { execSync } = require('node:child_process');
11+
12+
function isPortInUse(port) {
13+
try {
14+
const out = execSync(`lsof -nPiTCP -sTCP:LISTEN | grep :${port}\\>`, { stdio: ['ignore', 'pipe', 'ignore'] }).toString();
15+
return out.trim().length > 0;
16+
} catch {
17+
// grep exits non-zero if not found
18+
return false;
19+
}
20+
}
21+
22+
function forceKillPort(port) {
23+
try { execSync(`lsof -ti:${port} | xargs kill -9`, { stdio: 'ignore' }); } catch {}
24+
try { execSync(`fuser -k ${port}/tcp`, { stdio: 'ignore' }); } catch {}
25+
}
26+
27+
async function ensurePortFree(port, timeoutMs = 15000) {
28+
const start = Date.now();
29+
while (Date.now() - start < timeoutMs) {
30+
if (!isPortInUse(port)) return;
31+
forceKillPort(port);
32+
await delay(500);
33+
}
34+
}
1035

1136
const root = path.resolve(__dirname, '..');
1237

@@ -177,6 +202,8 @@ async function main() {
177202
});
178203
}
179204

205+
console.log(`[federated-css] ensuring port ${port} is free for ${label}...`);
206+
await ensurePortFree(port, 20000);
180207
console.log(`[federated-css] starting ${label} with next start...`);
181208
const proc = run('pnpm', ['-C', cwd, 'exec', 'next', 'start', '-p', String(port)]);
182209
procs.push(proc);

0 commit comments

Comments
 (0)