Skip to content

FIX: Handle undecodable netstat output in stop-server on non-English Windows - #4697

Open
Bharath Vemula (bharathvemula) wants to merge 1 commit into
microsoft:mainfrom
bharathvemula:fix/4696-netstat-utf8-decode-forkbase
Open

Bharath Vemula (bharathvemula) wants to merge 1 commit into
microsoft:mainfrom
bharathvemula:fix/4696-netstat-utf8-decode-forkbase

Conversation

@bharathvemula

Copy link
Copy Markdown

Description

On a non-English Windows install with Python in UTF-8 mode, pyrit_scan stop-server crashed with AttributeError: 'NoneType' object has no attribute 'splitlines'.

_find_pid_on_port_windows in pyrit/cli/_server_launcher.py ran netstat with text=True and no encoding/errors. netstat prints localized column headers in the OEM code page, so decoding as UTF-8 fails and subprocess.run returns stdout=None with return code 0 — which _parse_netstat_listener_pids then called .splitlines() on.

  • Decode with errors="replace" on both subprocess.run calls in _find_pid_on_port_windows (netstat and the sibling PowerShell lookup, which has the same latent crash). Only the ASCII port/PID tokens are needed, so replacing undecodable bytes is enough.
  • Treat a missing stdout as no output (result.stdout or "") so the lookup can never crash on this outcome again.
  • The Unix helpers (lsof/ss) are untouched — ASCII output, different code path.

Fixes #4696.

Tests

  • Added 3 regression tests in tests/unit/cli/test_server_launcher.py:
    • test_windows_pid_lookup_tolerates_undecodable_output — a fake subprocess.run that decodes raw bytes per the requested errors mode (strict returns stdout=None, as observed on CPython 3.14; replace yields U+FFFD). The lookup finds the listening PID and both calls pass errors="replace".
    • test_windows_pid_lookup_returns_none_for_free_port_with_undecodable_output — the exact reported scenario (free port after the server stopped) returns None instead of crashing.
    • test_parse_netstat_listener_pids_tolerates_replacement_characters — the parser finds the PID when the header is replacement characters.
  • Before the fix, the two lookup tests fail with AttributeError at result.stdout.splitlines(); after the fix, all 3 pass.
  • Full file: tests/unit/cli/test_server_launcher.py — 101 passed, 1 skipped.
  • ruff check and ruff format --check clean on both touched files.

Note: this PR was prepared with AI assistance (Muse). The fix follows the change suggested in the issue and was verified by the tests above: the new tests fail on the unmodified code and pass with the fix.

…Windows

_find_pid_on_port_windows ran netstat with text=True and no encoding, so
on non-English Windows in UTF-8 mode the localized output failed to
decode, subprocess.run returned stdout=None, and parsing it raised
AttributeError. Decode with errors=replace (only the ASCII port/PID
tokens are needed) and treat a missing stdout as no output, on both the
netstat and the sibling PowerShell lookups in the same function.

Fixes microsoft#4696
Copilot AI balanced review requested due to automatic review settings November 27, 2026 12:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block approval of the Windows process-lookup decoding changes.

No open issues 🎉

What changed in this PR

Fixes #4696 by making Windows process lookup tolerate undecodable command output, preventing pyrit_scan stop-server from crashing on non-English Windows in UTF-8 mode.

Changes:

  • Uses replacement decoding for PowerShell and netstat output.
  • Treats missing standard output as empty; Unix lookup helpers remain unchanged.
  • Adds three regression tests covering localized output, free ports, and replacement characters.
File Description
tests/​unit/​cli/​test_server_launcher.py Adds regression coverage for undecodable Windows command output.
pyrit/​cli/​_server_launcher.py Makes Windows process lookup resilient to decoding failures and missing output.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: stop-server crashes on non-English Windows in UTF-8 mode (netstat output decoded as UTF-8)

2 participants