Skip to content

fix(core): wrap print calls with try-except OSError#1358

Closed
mr-emmett-one wants to merge 1 commit into
NousResearch:mainfrom
mr-emmett-one:fix/prevent-stdout-crash-845
Closed

fix(core): wrap print calls with try-except OSError#1358
mr-emmett-one wants to merge 1 commit into
NousResearch:mainfrom
mr-emmett-one:fix/prevent-stdout-crash-845

Conversation

@mr-emmett-one

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR wraps multiple print() calls within the run_conversation function in run_agent.py with try...except OSError blocks.

It prevents the agent from crashing with a Broken Pipe or Input/output error when running in headless environments (like Docker containers, systemd services, or remote CI/CD) where a terminal (stdout) might be unavailable or disconnected.

Why is this necessary?

Currently, the agent's core loop can fail entirely if it attempts to output progress messages to a non-existent terminal. This change ensures that the agent's logic remains robust and continues to execute even if visual output fails.

Related Issue

Fixes #845

Prevents agent crashes in headless environments (Docker, systemd) where stdout might be unavailable. Fixes NousResearch#845.
@teknium1

Copy link
Copy Markdown
Contributor

The approach is right but there's a bug: the key = self._anthropic_api_key and auth_method = ... assignments were removed from the diff but are still referenced inside the try block. This would cause a NameError at runtime.

Fix: keep those two lines before the try block. The try-except should only wrap the print() calls, not the variable assignments.

teknium1 added a commit that referenced this pull request Mar 17, 2026
In headless environments (systemd, Docker, nohup) stdout can become
unavailable mid-session. Raw print() raises OSError which crashes
cron jobs — agent finishes work but delivery never happens because
the error handler's own print() also raises OSError.

Fix:
- Add _safe_print() static method that wraps print() with try/except
  OSError — silently drops output when stdout is broken
- Make _vprint() use _safe_print() — protects all calls through the
  verbose print path
- Convert raw print() calls in run_conversation() hot path to use
  _safe_print(): starting conversation, interrupt, budget exhausted,
  preflight compression, context cache, conversation completed
- Error handler print (the cascading crash point) gets explicit
  try/except with logger.error() fallback so diagnostics aren't lost

Fixes #845
Closes #1358 (superseded — PR was 323 commits stale with a bug)
@teknium1 teknium1 closed this in a3ac142 Mar 17, 2026
angelburgosrosado pushed a commit to angelburgosrosado/hermes-agent that referenced this pull request Apr 27, 2026
In headless environments (systemd, Docker, nohup) stdout can become
unavailable mid-session. Raw print() raises OSError which crashes
cron jobs — agent finishes work but delivery never happens because
the error handler's own print() also raises OSError.

Fix:
- Add _safe_print() static method that wraps print() with try/except
  OSError — silently drops output when stdout is broken
- Make _vprint() use _safe_print() — protects all calls through the
  verbose print path
- Convert raw print() calls in run_conversation() hot path to use
  _safe_print(): starting conversation, interrupt, budget exhausted,
  preflight compression, context cache, conversation completed
- Error handler print (the cascading crash point) gets explicit
  try/except with logger.error() fallback so diagnostics aren't lost

Fixes NousResearch#845
Closes NousResearch#1358 (superseded — PR was 323 commits stale with a bug)
02356abc pushed a commit to 02356abc/hermes-agent that referenced this pull request May 14, 2026
In headless environments (systemd, Docker, nohup) stdout can become
unavailable mid-session. Raw print() raises OSError which crashes
cron jobs — agent finishes work but delivery never happens because
the error handler's own print() also raises OSError.

Fix:
- Add _safe_print() static method that wraps print() with try/except
  OSError — silently drops output when stdout is broken
- Make _vprint() use _safe_print() — protects all calls through the
  verbose print path
- Convert raw print() calls in run_conversation() hot path to use
  _safe_print(): starting conversation, interrupt, budget exhausted,
  preflight compression, context cache, conversation completed
- Error handler print (the cascading crash point) gets explicit
  try/except with logger.error() fallback so diagnostics aren't lost

Fixes NousResearch#845
Closes NousResearch#1358 (superseded — PR was 323 commits stale with a bug)
olympus-terminal pushed a commit to olympus-terminal/hermes-agent that referenced this pull request May 16, 2026
In headless environments (systemd, Docker, nohup) stdout can become
unavailable mid-session. Raw print() raises OSError which crashes
cron jobs — agent finishes work but delivery never happens because
the error handler's own print() also raises OSError.

Fix:
- Add _safe_print() static method that wraps print() with try/except
  OSError — silently drops output when stdout is broken
- Make _vprint() use _safe_print() — protects all calls through the
  verbose print path
- Convert raw print() calls in run_conversation() hot path to use
  _safe_print(): starting conversation, interrupt, budget exhausted,
  preflight compression, context cache, conversation completed
- Error handler print (the cascading crash point) gets explicit
  try/except with logger.error() fallback so diagnostics aren't lost

Fixes NousResearch#845
Closes NousResearch#1358 (superseded — PR was 323 commits stale with a bug)
gweeteve pushed a commit to gweeteve/hermes-agent that referenced this pull request Jun 2, 2026
In headless environments (systemd, Docker, nohup) stdout can become
unavailable mid-session. Raw print() raises OSError which crashes
cron jobs — agent finishes work but delivery never happens because
the error handler's own print() also raises OSError.

Fix:
- Add _safe_print() static method that wraps print() with try/except
  OSError — silently drops output when stdout is broken
- Make _vprint() use _safe_print() — protects all calls through the
  verbose print path
- Convert raw print() calls in run_conversation() hot path to use
  _safe_print(): starting conversation, interrupt, budget exhausted,
  preflight compression, context cache, conversation completed
- Error handler print (the cascading crash point) gets explicit
  try/except with logger.error() fallback so diagnostics aren't lost

Fixes NousResearch#845
Closes NousResearch#1358 (superseded — PR was 323 commits stale with a bug)
Egavasyug pushed a commit to Egavasyug/hermes-agent that referenced this pull request Jun 10, 2026
In headless environments (systemd, Docker, nohup) stdout can become
unavailable mid-session. Raw print() raises OSError which crashes
cron jobs — agent finishes work but delivery never happens because
the error handler's own print() also raises OSError.

Fix:
- Add _safe_print() static method that wraps print() with try/except
  OSError — silently drops output when stdout is broken
- Make _vprint() use _safe_print() — protects all calls through the
  verbose print path
- Convert raw print() calls in run_conversation() hot path to use
  _safe_print(): starting conversation, interrupt, budget exhausted,
  preflight compression, context cache, conversation completed
- Error handler print (the cascading crash point) gets explicit
  try/except with logger.error() fallback so diagnostics aren't lost

Fixes NousResearch#845
Closes NousResearch#1358 (superseded — PR was 323 commits stale with a bug)
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)

2 participants