From 2a0e8b001f67b568abab6f876268bdeeddc98037 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Sun, 29 Mar 2026 18:21:27 -0700 Subject: [PATCH] fix(cli): handle closed stdout ValueError in safe print paths (#3843) When stdout is closed (piped to a dead process, broken terminal), Python raises ValueError('I/O operation on closed file'), not OSError. _safe_print and the API error printer only caught OSError, letting the ValueError propagate and crash the agent. Salvaged from PR #3760 by @apexscaleai. Fixes #3534. Co-authored-by: apexscaleai --- run_agent.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/run_agent.py b/run_agent.py index 32661a1b7..30453c01c 100644 --- a/run_agent.py +++ b/run_agent.py @@ -1285,7 +1285,7 @@ class AIAgent: try: fn = self._print_fn or print fn(*args, **kwargs) - except OSError: + except (OSError, ValueError): pass def _vprint(self, *args, force: bool = False, **kwargs): @@ -7909,7 +7909,7 @@ class AIAgent: error_msg = f"Error during OpenAI-compatible API call #{api_call_count}: {str(e)}" try: print(f"❌ {error_msg}") - except OSError: + except (OSError, ValueError): logger.error(error_msg) if self.verbose_logging: