fix(gateway): tolerate Unicode in stderr log handlers on Windows#40500
Open
annguyenNous wants to merge 1 commit into
Open
fix(gateway): tolerate Unicode in stderr log handlers on Windows#40500annguyenNous wants to merge 1 commit into
annguyenNous wants to merge 1 commit into
Conversation
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
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.
Bug
On Windows with non-UTF-8 console encodings (e.g. cp949, cp1252),
StreamHandleremits raiseUnicodeEncodeErrorwhen log messages contain characters outside the console codepage — such as the em-dash (U+2014) in the session hygiene message:This silently kills the gateway process with no diagnostic output.
Fix
Add
_safe_stderr()helper inhermes_logging.pythat wrapssys.stderrin aTextIOWrapperwithencoding='utf-8'anderrors='replace'when the console encoding is not UTF-8.Applied to both:
hermes_logging.py—setup_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 insetup_verbose_logging()gateway/run.py— imported and used_safe_stderr()for stderr handlertests/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/macOStest_wraps_non_utf8_stderr— wraps stderr with UTF-8 on legacy encodingstest_handler_emits_unicode_without_crash— em-dash logging worksFixes #40432