Repository navigation
FIX: Handle undecodable netstat output in stop-server on non-English Windows - #4697
Open
Bharath Vemula (bharathvemula) wants to merge 1 commit into
Open
Bharath Vemula (bharathvemula) wants to merge 1 commit into
Bharath Vemula (bharathvemula) wants to merge 1 commit into
Conversation
…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
Contributor
There was a problem hiding this comment.
🟢 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
On a non-English Windows install with Python in UTF-8 mode,
pyrit_scan stop-servercrashed withAttributeError: 'NoneType' object has no attribute 'splitlines'._find_pid_on_port_windowsinpyrit/cli/_server_launcher.pyrannetstatwithtext=Trueand noencoding/errors.netstatprints localized column headers in the OEM code page, so decoding as UTF-8 fails andsubprocess.runreturnsstdout=Nonewith return code 0 — which_parse_netstat_listener_pidsthen called.splitlines()on.errors="replace"on bothsubprocess.runcalls 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.stdoutas no output (result.stdout or "") so the lookup can never crash on this outcome again.lsof/ss) are untouched — ASCII output, different code path.Fixes #4696.
Tests
tests/unit/cli/test_server_launcher.py:test_windows_pid_lookup_tolerates_undecodable_output— a fakesubprocess.runthat decodes raw bytes per the requestederrorsmode (strict returnsstdout=None, as observed on CPython 3.14;replaceyields U+FFFD). The lookup finds the listening PID and both calls passerrors="replace".test_windows_pid_lookup_returns_none_for_free_port_with_undecodable_output— the exact reported scenario (free port after the server stopped) returnsNoneinstead of crashing.test_parse_netstat_listener_pids_tolerates_replacement_characters— the parser finds the PID when the header is replacement characters.AttributeErroratresult.stdout.splitlines(); after the fix, all 3 pass.tests/unit/cli/test_server_launcher.py— 101 passed, 1 skipped.ruff checkandruff format --checkclean 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.