Skip to content

fix(gateway): handle wmic UnicodeDecodeError gracefully#17073

Closed
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/issue-17049-wmic-encoding-error
Closed

fix(gateway): handle wmic UnicodeDecodeError gracefully#17073
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/issue-17049-wmic-encoding-error

Conversation

@liuhao1024

Copy link
Copy Markdown
Contributor

Summary

Fixes UnicodeDecodeError crash in Windows gateway process scanning by adding explicit encoding and error handling to the wmic subprocess.run call.

Root Cause

On Windows systems, especially with non-English locales, the wmic command outputs text in the system's code page (e.g., cp936 for Chinese, cp1252 for Western European), not UTF-8.

The current code in hermes_cli/gateway.py (lines 278-283) calls subprocess.run with text=True, which implicitly uses UTF-8 encoding. When wmic output contains non-UTF-8 bytes:

  1. Python's subprocess raises UnicodeDecodeError in the reader thread
  2. result.stdout becomes None after decode failure
  3. Line 287 crashes with AttributeError: 'NoneType' object has no attribute 'split'

Fix

  • Add explicit encoding='utf-8' to subprocess.run call
  • Add errors='ignore' to skip undecodable bytes instead of crashing
  • Check result.stdout is None before splitting to avoid AttributeError

This allows the setup wizard to continue gracefully on systems where wmic output cannot be fully decoded, returning an empty PID list instead of crashing.

Testing

  • Added regression test TestScanGatewayPids::test_handles_wmic_unicode_decode_error_gracefully that simulates decode failure (stdout=None) and verifies empty list is returned
  • Existing test test_find_gateway_pids_falls_back_to_pid_file_when_process_scan_fails still passes

Impact

  • Fixes crash during hermes setup on Windows with non-English locales
  • No functional regression: setup wizard continues with empty PID scan, which falls back to PID file discovery
  • Small, focused change: 3 lines in gateway.py, 1 test class

Fixes #17049

On Windows with non-English locales, wmic output uses system code page
(e.g., cp936, cp1252) instead of UTF-8. When subprocess.run
with text=True tries to decode as UTF-8, it raises UnicodeDecodeError
and result.stdout becomes None, causing AttributeError in the next line.

Fixes:
- Add explicit encoding='utf-8' to wmic subprocess.run call
- Add errors='ignore' to skip undecodable bytes instead of crashing
- Check result.stdout is None before splitting to avoid AttributeError

Adds regression test for decode error scenario.

Fixes NousResearch#17049
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/cli CLI entry point, hermes_cli/, setup wizard comp/gateway Gateway runner, session dispatch, delivery duplicate This issue or pull request already exists labels Apr 28, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Competing fix with #17074 — both address #17049 (wmic UnicodeDecodeError on Windows non-UTF-8 locales). This PR includes a regression test; #17074 does not. Maintainer should pick one.

@liuhao1024

Copy link
Copy Markdown
Contributor Author

Closing this to avoid a competing duplicate PR since #17074 is already covering the same fix for #17049 (wmic UnicodeDecodeError on Windows).

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

Labels

comp/cli CLI entry point, hermes_cli/, setup wizard comp/gateway Gateway runner, session dispatch, delivery duplicate This issue or pull request already exists P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: UnicodeDecodeError and AttributeError in Windows gateway process scanning

2 participants