Skip to content

fix(gateway): use ctypes.OpenProcess for Windows process existence check#13587

Closed
zhanggttry wants to merge 3 commits into
NousResearch:mainfrom
zhanggttry:fix/windows-os-kill-5760
Closed

fix(gateway): use ctypes.OpenProcess for Windows process existence check#13587
zhanggttry wants to merge 3 commits into
NousResearch:mainfrom
zhanggttry:fix/windows-os-kill-5760

Conversation

@zhanggttry

Copy link
Copy Markdown
Contributor

Summary

Fixes #5760, #8978, #12359os.kill(pid, 0) raises SystemError: [WinError 87] on Windows, blocking gateway startup.

Root Cause

Windows does not support Unix signal 0 (existence check). os.kill(pid, 0) on Windows raises:

SystemError: <built-in function kill> returned a result with an exception set
OSError: [WinError 87] The parameter is incorrect

This crashes gateway startup at two locations:

  • acquire_scoped_lock() (line 363) — blocks Telegram platform connection
  • get_running_pid() (line 598) — blocks gateway startup entirely

Solution

Add _process_exists(pid: int) -> bool helper function that uses:

  • Windows: ctypes.windll.kernel32.OpenProcess() — native Windows API for process existence check
  • POSIX: os.kill(pid, 0) — unchanged behavior

Changes

  • gateway/status.py: Add _process_exists() helper, replace 2 os.kill(pid, 0) calls

Testing

  • Windows: Verify gateway starts without SystemError
  • POSIX: Verify no regression in process detection

Related Issues

Replace os.kill(pid, 0) with _process_exists() helper that uses:
- Windows: ctypes.windll.kernel32.OpenProcess() (avoids SystemError WinError 87)
- POSIX: os.kill(pid, 0) (unchanged behavior)

Fixes NousResearch#5760, NousResearch#8978, NousResearch#12359
@alt-glitch alt-glitch added type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery labels Apr 21, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Related to #9024, #13198, #5762 — all fix the same os.kill(pid,0) Windows crash in gateway/status.py with different approaches. Consider consolidating.

zhanggttry added a commit to zhanggttry/hermes-agent that referenced this pull request Apr 22, 2026
…g to file reads

- os.kill(pid, 0) on Windows raises OSError (WinError 87) for non-existent
  PIDs instead of ProcessLookupError. Catch OSError everywhere to prevent
  crash on Windows process-existence checks.

- Path.read_text() and open() default to gbk on Chinese Windows. Add
  explicit encoding='utf-8' to all file reads to prevent UnicodeDecodeError
  when config files or skill manifests contain non-ASCII characters.

Files changed:
- gateway/run.py, gateway/status.py (os.kill + read_text)
- hermes_cli/*.py (read_text + open)
- tools/*.py (read_text)

Closes: NousResearch#13587 NousResearch#5762 NousResearch#7835 NousResearch#9024
@zhanggttry

Copy link
Copy Markdown
Contributor Author

Superseded by #13986 which uses a safer pure-Python approach (no ctypes/windll) to avoid Supply Chain Audit failures.

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

Labels

comp/gateway Gateway runner, session dispatch, delivery type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: os.kill(pid, 0) raises SystemError/OSError in gateway/status.py

2 participants