Skip to content

Commit 7173b1b

Browse files
committed
fix(client): find Windows port owners without netstat text
The Windows listener check parsed netstat and matched the STATE column against "LISTENING". Windows localizes that column (ABHOREN, ECOUTE), so on a non-English install ownsListeningPort never found the owner. Recovery then only deleted the registration instead of terminating the stuck service, and the replacement could not bind the port the stuck service still held. Ask the OS instead: Get-NetTCPConnection -State Listen -LocalPort <port> and read OwningProcess. Cmdlet and parameter names are not localized, the result is structured, and the query is scoped to one port, so a busy machine cannot overflow the output. This runs only after the owner has been silent past the recovery window, so a cold PowerShell start is off any latency-sensitive path; the timeout is 5s to cover it.
1 parent 6deb880 commit 7173b1b

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

‎packages/client/src/service-probe.ts‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,27 @@ async function linuxOwnsListener(pid: number, port: number) {
201201
}
202202

203203
async function windowsListeners(port: number) {
204+
// Ask Windows for the socket's owner instead of parsing netstat. netstat localizes its state
205+
// column ("ABHÖREN", "ÉCOUTE", ...), so matching "LISTENING" finds nothing on non-English
206+
// Windows and a stuck service keeps the managed port, which makes the replacement fail to
207+
// bind. The cmdlet and parameter names are not localized. This runs only while recovering an
208+
// owner that has been silent past the recovery window, so the PowerShell start is not on a
209+
// latency-sensitive path.
204210
try {
205-
const result = await promisify(execFile)("netstat", ["-ano", "-p", "tcp"], {
206-
timeout: 1_000,
207-
maxBuffer: 1_048_576,
208-
windowsHide: true,
209-
})
211+
const result = await promisify(execFile)(
212+
"powershell.exe",
213+
[
214+
"-NoProfile",
215+
"-NonInteractive",
216+
"-Command",
217+
`(Get-NetTCPConnection -State Listen -LocalPort ${port} -ErrorAction SilentlyContinue).OwningProcess`,
218+
],
219+
{ timeout: 5_000, maxBuffer: 4_096, windowsHide: true },
220+
)
210221
return result.stdout
211-
.split("\n")
212-
.map((line) => line.trim().split(/\s+/))
213-
.filter((fields) => fields[0] === "TCP" && fields[3] === "LISTENING" && fields[1]?.endsWith(`:${port}`))
214-
.map((fields) => Number(fields[4]))
222+
.split(/\s+/)
223+
.filter(Boolean)
224+
.map(Number)
215225
.filter(Number.isInteger)
216226
} catch {
217227
return []

0 commit comments

Comments
 (0)