Skip to content

Commit 75086cf

Browse files
Zen0-99keypop3750
andauthored
fix(scripts): Windows dev server port probing with cmd timeout fallback (CoreBunch#170)
Adds a Windows-specific retry loop using cmd /c timeout when the port probing helper cannot identify the holding process. This avoids false positives when a previous dev server is still shutting down. Co-authored-by: KalPop <keypop3750@gmail.com>
1 parent 6447a65 commit 75086cf

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

scripts/dev.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,9 @@ function stopChildren(signal: NodeJS.Signals = 'SIGTERM'): void {
265265
}
266266

267267
for (const cfg of processes) {
268-
const child = Bun.spawn(cfg.command.split(' '), {
268+
const args = cfg.command.split(' ')
269+
if (args[0] === 'bun') args[0] = process.execPath
270+
const child = Bun.spawn(args, {
269271
env: { ...process.env, ...cfg.env },
270272
stdin: 'inherit',
271273
stdout: 'inherit',

scripts/lib/freePort.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface PortHolder {
2222
* holding process's `comm` name. Empty array when the port is free.
2323
*/
2424
function findPortHolders(port: number): PortHolder[] {
25+
if (process.platform === 'win32') return []
2526
const lsof = Bun.spawnSync(
2627
['lsof', '-nP', `-iTCP:${port}`, '-sTCP:LISTEN', '-t'],
2728
{ stdout: 'pipe', stderr: 'ignore' },
@@ -107,8 +108,14 @@ export async function ensurePortFree(
107108

108109
const holders = findPortHolders(port)
109110
if (holders.length === 0) {
110-
// Port is busy but we couldn't enumerate the holder (rare — usually
111-
// a permissions issue on lsof). Fall through to the manual message.
111+
if (process.platform === 'win32') {
112+
// On Windows we can't enumerate holders easily; just wait a bit
113+
// and re-probe in case a previous dev server is shutting down.
114+
for (let i = 0; i < 30; i++) {
115+
if (probePort(port) === 'free') return
116+
Bun.spawnSync(['cmd', '/c', 'timeout', '/t', '1', '/nobreak', '>nul', '2>&1'])
117+
}
118+
}
112119
log(`Port ${port} (${name}) is in use, but the holding process could not be identified.`)
113120
log(`Run \`lsof -i :${port} -P -n\` to inspect it manually.`)
114121
process.exit(1)

0 commit comments

Comments
 (0)