Skip to content

fix(gateway): tolerate Unicode in stderr log handlers on Windows#40500

Open
annguyenNous wants to merge 1 commit into
NousResearch:mainfrom
annguyenNous:fix/windows-unicode-logging
Open

fix(gateway): tolerate Unicode in stderr log handlers on Windows#40500
annguyenNous wants to merge 1 commit into
NousResearch:mainfrom
annguyenNous:fix/windows-unicode-logging

Conversation

@annguyenNous
Copy link
Copy Markdown
Contributor

Bug

On Windows with non-UTF-8 console encodings (e.g. cp949, cp1252), StreamHandler emits raise UnicodeEncodeError when log messages contain characters outside the console codepage — such as the em-dash (U+2014) in the session hygiene message:

UnicodeEncodeError: 'cp949' codec can't encode character '\u2014'
Message: 'Session hygiene: %s messages, ~%s tokens (%s) — auto-compressing ...'

This silently kills the gateway process with no diagnostic output.

Fix

Add _safe_stderr() helper in hermes_logging.py that wraps sys.stderr in a TextIOWrapper with encoding='utf-8' and errors='replace' when the console encoding is not UTF-8.

Applied to both:

  • hermes_logging.pysetup_verbose_logging() stderr handler (line 337)
  • gateway/run.py — optional stderr handler (line 19711)

The wrapper ensures log lines are never lost — un-encodable characters are replaced with ? instead of crashing the process.

Files changed

  • hermes_logging.py — added _safe_stderr(), used in setup_verbose_logging()
  • gateway/run.py — imported and used _safe_stderr() for stderr handler
  • tests/test_hermes_logging.py — 3 new tests for _safe_stderr()

Tests

All 3 new tests pass:

  • test_returns_stderr_on_utf8_system — no-op on Linux/macOS
  • test_wraps_non_utf8_stderr — wraps stderr with UTF-8 on legacy encodings
  • test_handler_emits_unicode_without_crash — em-dash logging works

Fixes #40432

On Windows with non-UTF-8 console encodings (e.g. cp949, cp1252),
StreamHandler emits raise UnicodeEncodeError when log messages contain
characters outside the console codepage — such as the em-dash (U+2014)
in the session hygiene message.

This crashed the gateway process silently, leaving no diagnostic output.

Fix: add _safe_stderr() helper that wraps sys.stderr in a TextIOWrapper
with encoding='utf-8' and errors='replace' when the console encoding
is not UTF-8.  Applied to both:
- hermes_logging.py setup_verbose_logging() stderr handler
- gateway/run.py optional stderr handler

The wrapper ensures log lines are never lost — un-encodable characters
are replaced with '?' instead of crashing the process.

Fixes NousResearch#40432
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery labels Jun 6, 2026
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 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.

Windows gateway logging should tolerate Unicode in rotating log handlers

2 participants