Skip to content

fix: guard all print() against OSError with _SafeWriter#963

Merged
teknium1 merged 1 commit intomainfrom
hermes/hermes-cf9f7d54
Mar 11, 2026
Merged

fix: guard all print() against OSError with _SafeWriter#963
teknium1 merged 1 commit intomainfrom
hermes/hermes-cf9f7d54

Conversation

@teknium1
Copy link
Contributor

Problem

When hermes-agent runs as a systemd service or headless daemon, stdout can become unavailable (idle timeout, buffer exhaustion, socket reset). Any print() raises OSError: [Errno 5], crashing run_conversation() — especially via double-fault when the except handler also tries to print (#845).

PR #858 attempted to fix this by wrapping 7 individual print() calls in try/except, but run_conversation() has 68 print() calls and the PR left adjacent prints in the same blocks unguarded.

Fix

A transparent _SafeWriter wrapper installed once at the start of run_conversation(). It delegates all writes to the real stdout and silently catches OSError.

  • Zero overhead when stdout is healthy (just a normal method delegation)
  • Comprehensive coverage of all 68+ print calls, including future ones
  • isinstance guard prevents double-wrapping across multiple run_conversation() calls
  • 6 tests: normal delegation, OSError catch on write/flush, print survival, installation verification, double-wrap prevention

Fixes #845

Co-authored-by: J0hnLawMississippi J0hnLawMississippi@users.noreply.github.com

When hermes-agent runs as a systemd service, Docker container, or
headless daemon, the stdout pipe can become unavailable (idle timeout,
buffer exhaustion, socket reset). Any print() call then raises
OSError: [Errno 5] Input/output error, crashing run_conversation()
and causing cron jobs to fail.

Rather than wrapping individual print() calls (68 in run_conversation
alone), this adds a transparent _SafeWriter wrapper installed once at
the start of run_conversation(). It delegates all writes to the real
stdout and silently catches OSError. Zero overhead on the happy path,
comprehensive coverage of all print calls including future ones.

Fixes #845

Co-authored-by: J0hnLawMississippi <J0hnLawMississippi@users.noreply.github.com>
@teknium1 teknium1 merged commit 8fa96de into main Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: guard print() calls in run_conversation() against OSError when stdout is unavailable (systemd/headless)

1 participant