From 474301adc6c1903a3944c32cf0bb1c256a0d2083 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Mon, 16 Mar 2026 23:13:26 -0700 Subject: [PATCH] fix: improve execute_code error logging and harden cleanup (#1623) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(tools): improve error logging in code_execution_tool * fix: harden execute_code cleanup and reduce logging noise Follow-up to cherry-picked PR #1588 (aydnOktay): - Initialize server_sock = None before try block to prevent NameError if exception occurs before socket creation (line 413 is inside the try) - Guard server_sock.close() with None check - Narrow cleanup exception handlers to OSError (the actual error type) - Remove exc_info=True from cleanup debug logs — benign teardown failures don't need stack traces, the message is sufficient - Remove redundant try/except around shutil.rmtree(ignore_errors=True) - Silence sock_path unlink with pass — expected when already cleaned up --------- Co-authored-by: aydnOktay --- tools/code_execution_tool.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/tools/code_execution_tool.py b/tools/code_execution_tool.py index 6c3f19b33..1e97156ec 100644 --- a/tools/code_execution_tool.py +++ b/tools/code_execution_tool.py @@ -395,6 +395,7 @@ def execute_code( tool_call_log: list = [] tool_call_counter = [0] # mutable so the RPC thread can increment exec_start = time.monotonic() + server_sock = None try: # Write the auto-generated hermes_tools module @@ -598,7 +599,14 @@ def execute_code( except Exception as exc: duration = round(time.monotonic() - exec_start, 2) - logging.exception("execute_code failed") + logger.error( + "execute_code failed after %ss with %d tool calls: %s: %s", + duration, + tool_call_counter[0], + type(exc).__name__, + exc, + exc_info=True, + ) return json.dumps({ "status": "error", "error": str(exc), @@ -608,19 +616,17 @@ def execute_code( finally: # Cleanup temp dir and socket - try: - server_sock.close() - except Exception as e: - logger.debug("Server socket close error: %s", e) - try: - import shutil - shutil.rmtree(tmpdir, ignore_errors=True) - except Exception as e: - logger.debug("Could not clean temp dir: %s", e, exc_info=True) + if server_sock is not None: + try: + server_sock.close() + except OSError as e: + logger.debug("Server socket close error: %s", e) + import shutil + shutil.rmtree(tmpdir, ignore_errors=True) try: os.unlink(sock_path) - except OSError as e: - logger.debug("Could not remove socket file: %s", e, exc_info=True) + except OSError: + pass # already cleaned up or never created def _kill_process_group(proc, escalate: bool = False):