Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions openflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* `packages/flow/lib/launch.ts` (unit-tested); this file only spawns, streams,
* waits, and tears down.
*/
import { launchPlan } from "./packages/flow/lib/launch"
import { launchPlan, listeningPids } from "./packages/flow/lib/launch"
import { healthy } from "./packages/flow/lib/opencode-process"

const repo = import.meta.dir
Expand Down Expand Up @@ -56,15 +56,7 @@ async function pipe(stream: ReadableStream<Uint8Array> | number | undefined, pre
function freePort(port: string) {
if (process.platform === "win32") {
const { stdout } = Bun.spawnSync(["netstat", "-ano"], { stdout: "pipe" })
const pids = new Set(
stdout
.toString()
.split(/\r?\n/)
.filter((line) => line.includes(`:${port}`) && line.includes("LISTENING"))
.map((line) => line.trim().split(/\s+/).pop())
.filter((pid): pid is string => !!pid && pid !== "0"),
)
for (const pid of pids) Bun.spawnSync(["taskkill", "/pid", pid, "/T", "/F"], { stdout: "ignore", stderr: "ignore" })
for (const pid of listeningPids(stdout.toString(), port)) Bun.spawnSync(["taskkill", "/pid", pid, "/T", "/F"], { stdout: "ignore", stderr: "ignore" })
return
}
const { stdout } = Bun.spawnSync(["lsof", "-ti", `tcp:${port}`], { stdout: "pipe" })
Expand Down
29 changes: 28 additions & 1 deletion packages/flow/lib/launch.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test"
import { launchPlan } from "./launch"
import { launchPlan, listeningPids } from "./launch"

const repo = "/repo"

Expand Down Expand Up @@ -103,3 +103,30 @@ describe("launchPlan", () => {
expect(plan.engine.skip).toBe(false)
})
})

describe("listeningPids", () => {
const netstat = [
"Active Connections",
"",
" Proto Local Address Foreign Address State PID",
" TCP 0.0.0.0:4096 0.0.0.0:0 LISTENING 100",
" TCP [::]:4096 [::]:0 LISTENING 100",
" TCP 0.0.0.0:40961 0.0.0.0:0 LISTENING 200",
" TCP 127.0.0.1:51234 127.0.0.1:4096 ESTABLISHED 300",
" TCP 0.0.0.0:5174 0.0.0.0:0 ABHÖREN 400",
" UDP 0.0.0.0:4096 *:* 500",
].join("\r\n")

test("matches the port exactly, not as a prefix of a longer one", () => {
expect(listeningPids(netstat, "4096")).toEqual(["100"])
expect(listeningPids(netstat, "409")).toEqual([])
})

test("ignores a connection whose remote end is the port", () => {
expect(listeningPids(netstat, "51234")).toEqual([])
})

test("finds a listener whatever language the state column is in", () => {
expect(listeningPids(netstat, "5174")).toEqual(["400"])
})
})
25 changes: 25 additions & 0 deletions packages/flow/lib/launch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,28 @@ export async function launchPlan(input: {
built,
}
}

/**
* PIDs listening on exactly `port`, read from Windows `netstat -ano` output.
*
* The port is compared against the local-address column rather than searched
* for in the line: `:4096` is a substring of `:40961`, so a substring test hands
* an unrelated process to `taskkill /T /F`. A listening socket is told by its
* foreign address (`0.0.0.0:0` or `[::]:0`), not by the state column, because
* that column is translated on non-English Windows.
*/
export function listeningPids(netstat: string, port: string) {
return [
...new Set(
netstat
.split(/\r?\n/)
.map((line) => line.trim().split(/\s+/))
.filter(
([proto, local, foreign]) =>
proto === "TCP" && local?.endsWith(`:${port}`) && (foreign === "0.0.0.0:0" || foreign === "[::]:0"),
)
.map((columns) => columns.at(-1))
.filter((pid): pid is string => !!pid && pid !== "0"),
),
]
}