Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(server): stop Windows port discovery thrashing WMI #6254

ClosedRioPlay(GitHub profile, opens in a new tab)wants to merge 3 commits intomain fromrefs/pull/6254/headopened View on GitHub

Problem

On busy Windows machines, preview port discovery re-spawns a PowerShell listener probe every few seconds. The probe called Get-Process -Id once per listening socket, which often exceeded the 5s timeout. Timed-out probes were killed mid-WMI query and retried forever, driving sustained WMI/CPU load (#5900).

Fix

  • Build a single PID→name map with one Get-Process per probe instead of per connection.
  • After a probe timeout, cool the expensive path off for ~60s and use the existing common-port fallback so a doomed probe is not re-spawned every poll.

Verification

  • vp test run apps/server/src/preview/PortScanner.test.ts (6 passed)
  • Live on Windows: Preview Local servers listed listening ports with process names (map-once path), including an intentional localhost:3000 listener.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • No UI changes

Model: Grok 4.5 · Harness: Grok Build / T3 Code


Note

Medium Risk Changes Windows process probing and port discovery behavior; during cooldown or concurrent scans, results may omit process names via the common-port fallback.

Overview Stops Windows preview port discovery from repeatedly spawning expensive PowerShell/WMI probes that were timing out and thrashing CPU (#5900).

Replaces per-listener Get-Process -Id with a single WINDOWS_LISTENER_COMMAND that builds the PID→name map once. Adds single-flight gating and a 60s wall-clock cooldown after timeouts so concurrent scan() / poll / retain paths fall back to common ports instead of stacking probes. Uses acquireUseRelease so the in-flight flag clears even if the probe fiber is interrupted.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 920f8251d9c63d0b7e78995ae4bcb3b65f5e6fbf. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Stop Windows port discovery from thrashing WMI with per-listener Get-Process calls

  • Replaces the per-connection Get-Process -Id PowerShell calls with a single PID-to-name map built once per probe via the new WINDOWS_LISTENER_COMMAND in PortScanner.ts.
  • Adds single-flight enforcement so at most one PowerShell probe runs at a time; concurrent scan() calls skip the probe and fall back to common-port results while the probe is in flight.
  • Arms a wall-clock cooldown (WINDOWS_LISTENER_COOLDOWN_MS) after a ProcessTimeoutError so subsequent scans skip PowerShell until the cooldown expires.
  • The in-flight flag is cleared via acquireUseRelease so fiber interruption cannot leave the gate permanently locked.
  • Tests covering all four behaviors (single map build, cooldown, single-flight, interrupted probe) are added in PortScanner.test.ts.
Macroscope summarized 920f825.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: 675912db-542f-4c13-a936-a2bdbe8f66e9

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

      Fix All in Cursor ❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot).

      Want fixes drafted automatically? Bugbot Autofix can create code changes for findings. A team admin can enable Autofix in the Cursor dashboard.

      Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit a78e13b87f058723ce73868c27971df45f4df04b. Configure [here](https://www.cursor.com/dashboard/bugbot).
    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Needs human review

      Performance optimization that changes the Windows PowerShell port probe to build a process-name map once and adds cooldown/single-flight mechanisms. While the changes are well-tested and focused, they modify core probing infrastructure with new state management, and the author is not a frequent contributor to this file.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    5. Addressing issues found by macroscopeapp and cursor... will submit changes soon.

    6. Pushed a follow-up commit that addresses the Macroscope/Bugbot findings on the first commit:

      • Wall-clock cooldown (windowsListenerCooldownUntilMs) instead of a tick counter, so retain/scan/poll cannot burn the cool-off early
      • Single-flight claim before spawning PowerShell, so overlapping poll + retain cannot both run the probe
      • Timeout still arms the ~60s cool-off; concurrent callers skip to the common-port fallback while a probe is in flight

      Tests: vp test run apps/server/src/preview/PortScanner.test.ts (7 passed), including concurrent scan() single-flight.

    7. Collapsing the per-listener Get-Process -Id into one map is clearly right, that was the quadratic part. Two things about the new gate though.

      The cooldown only arms on timeout, and spawn failure is the case that repeats forever.

      ProcessSpawnError: recoverWindowsProbeFailure,
      ...
      ProcessTimeoutError: (error) =>
        armWindowsListenerCooldown.pipe(Effect.zipRight(recoverWindowsProbeFailure(error))),

      A timeout is transient, and it gets the 60s cool-off. A spawn failure is usually not transient — powershell.exe blocked by policy, AppLocker, a stripped PATH — and it gets nothing, so the 3s poll retries the spawn indefinitely. That is a steadier drip of process creation than the timeout case this PR is fixing. Whatever the cool-off is worth after a timeout, it is worth more after a spawn error.

      While skipping, the scan publishes the common-port list rather than holding the last snapshot.

      skip returns null, and null falls through to probeCommonPorts(). So during the 60s cooldown, and during any single-flight overlap, every tick publishes only COMMON_DEV_PORTS instead of the real listener set. At a 3s interval that is roughly twenty ticks where a dev server on a non-common port drops out of the snapshot and then returns. Listeners see it as the server disappearing rather than as degraded discovery.

      Returning state.lastSnapshot on skip would keep the last good answer instead of replacing it with a narrower one. That also makes the distinction meaningful: common ports as a genuine fallback when there is nothing better, last snapshot when there is.

      Smaller notes:

      • Date.now() rather than Clock means the cooldown window cannot be driven by TestClock, so the 60s behaviour is hard to cover. The comment explains the wall-clock intent, and Clock.currentTimeMillis is also wall-clock under the live layer, so it would keep the intent and stay testable.
      • The title says WMI, but Get-NetTCPConnection is still a CIM cmdlet, so the WMI round trip stays and only its frequency drops. Worth wording so nobody later reads this as WMI having been removed from the path.
      • The branch is currently CONFLICTING against main.

      The acquireUseRelease comment explaining why it is not claim-plus-ensuring is a nice touch, that interrupt window is easy to miss.

    8. Note

      🤖 GPT-6 Astra (preview) responding on behalf of Theo

      This was closed as part of an automated cleanup pass. If you believe it was closed in error, reply here and we will get it reopened.

      Closing in favor of #9520, which replaces Windows PowerShell listener discovery with native enumeration. Review continues there, including Windows verification. That removes the polling path this branch tries to reduce. The listener PID mapping, interrupted scan recovery, and non-common-port fallback cases are recorded on the retained PR.

Sign in to join this conversation.