55 * stable line-prefixed field format; this is the only `lsof` flag set we rely
66 * on).
77 *
8- * Windows / lsof missing: checks a curated list of common dev ports through
9- * the shared Net service.
8+ * Windows: PowerShell lists listening sockets (one PID→name map per probe).
9+ * On timeout, fall back to the curated common-port list and cool the probe
10+ * briefly so a doomed WMI query is not re-spawned every poll (#5900).
11+ *
12+ * lsof missing / probe failed: checks a curated list of common dev ports
13+ * through the shared Net service.
1014 *
1115 * Polling is reference-counted via scoped `retain`. A single layer-scoped fiber
1216 * polls forever, but each tick is a no-op when the retain count is zero.
@@ -53,6 +57,15 @@ export const COMMON_DEV_PORTS: ReadonlyArray<number> = Object.freeze([
5357const POLL_INTERVAL = Duration . seconds ( 3 ) ;
5458const LSOF_TIMEOUT_MS = 5_000 ;
5559const WINDOWS_LISTENER_TIMEOUT_MS = 5_000 ;
60+ /** After a timeout, skip the expensive probe for this many poll ticks (~60s). */
61+ const WINDOWS_LISTENER_TIMEOUT_COOLDOWN_TICKS = 20 ;
62+
63+ /**
64+ * Windows listener probe command. Builds the process-name map once; per-listener
65+ * `Get-Process -Id` was the cost that made this miss its 5s timeout (#5900).
66+ */
67+ export const WINDOWS_LISTENER_COMMAND =
68+ '$m = @{}; Get-Process | ForEach-Object { $m[$_.Id] = $_.ProcessName }; Get-NetTCPConnection -State Listen -ErrorAction Stop | ForEach-Object { Write-Output "$($_.LocalAddress)|$($_.LocalPort)|$($_.OwningProcess)|$($m[[int]$_.OwningProcess])" }' ;
5669
5770type Listener = ( servers : ReadonlyArray < DiscoveredLocalServer > ) => Effect . Effect < void > ;
5871
@@ -67,6 +80,8 @@ interface ScannerState {
6780 }
6881 > ;
6982 readonly retainCount : number ;
83+ /** Poll ticks remaining before the Windows listener probe is tried again. */
84+ readonly windowsListenerCooldownTicks : number ;
7085}
7186
7287interface TerminalProcessOwner {
@@ -195,6 +210,7 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
195210 listeners : new Set ( ) ,
196211 terminalProcesses : new Map ( ) ,
197212 retainCount : 0 ,
213+ windowsListenerCooldownTicks : 0 ,
198214 } ) ;
199215
200216 const probeCommonPorts = Effect . fn ( "PortDiscovery.probeCommonPorts" ) ( function * ( ) {
@@ -238,13 +254,21 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
238254 }
239255 }
240256 if ( hostPlatform === "win32" ) {
257+ // A probe that just timed out will time out again until the machine
258+ // cools off; skip it for a while and use the cheap common-port path.
259+ if ( state . windowsListenerCooldownTicks > 0 ) {
260+ yield * Ref . update ( stateRef , ( current ) => ( {
261+ ...current ,
262+ windowsListenerCooldownTicks : Math . max ( 0 , current . windowsListenerCooldownTicks - 1 ) ,
263+ } ) ) ;
264+ return yield * probeCommonPorts ( ) ;
265+ }
266+
241267 const recoverWindowsProbeFailure = recoverProcessProbeFailure ( "windows-listeners" ) ;
242- const command =
243- 'Get-NetTCPConnection -State Listen -ErrorAction Stop | ForEach-Object { $processName = (Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue).ProcessName; Write-Output "$($_.LocalAddress)|$($_.LocalPort)|$($_.OwningProcess)|$processName" }' ;
244268 const listeners = yield * processRunner
245269 . run ( {
246270 command : "powershell.exe" ,
247- args : [ "-NoProfile" , "-NonInteractive" , "-Command" , command ] ,
271+ args : [ "-NoProfile" , "-NonInteractive" , "-Command" , WINDOWS_LISTENER_COMMAND ] ,
248272 timeout : Duration . millis ( WINDOWS_LISTENER_TIMEOUT_MS ) ,
249273 maxOutputBytes : 1024 * 1024 ,
250274 outputMode : "truncate" ,
@@ -256,7 +280,11 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
256280 ProcessStdinError : recoverWindowsProbeFailure ,
257281 ProcessOutputLimitError : recoverWindowsProbeFailure ,
258282 ProcessReadError : recoverWindowsProbeFailure ,
259- ProcessTimeoutError : recoverWindowsProbeFailure ,
283+ ProcessTimeoutError : ( error ) =>
284+ Ref . update ( stateRef , ( current ) => ( {
285+ ...current ,
286+ windowsListenerCooldownTicks : WINDOWS_LISTENER_TIMEOUT_COOLDOWN_TICKS ,
287+ } ) ) . pipe ( Effect . zipRight ( recoverWindowsProbeFailure ( error ) ) ) ,
260288 } ) ,
261289 ) ;
262290 if ( listeners !== null ) return listeners ;
0 commit comments