fix: improve execute_code error logging and harden cleanup (#1623)
* 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 <xaydinoktay@gmail.com>
This commit is contained in:
parent
cfa87e77a9
commit
474301adc6
1 changed files with 18 additions and 12 deletions
|
|
@ -395,6 +395,7 @@ def execute_code(
|
||||||
tool_call_log: list = []
|
tool_call_log: list = []
|
||||||
tool_call_counter = [0] # mutable so the RPC thread can increment
|
tool_call_counter = [0] # mutable so the RPC thread can increment
|
||||||
exec_start = time.monotonic()
|
exec_start = time.monotonic()
|
||||||
|
server_sock = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Write the auto-generated hermes_tools module
|
# Write the auto-generated hermes_tools module
|
||||||
|
|
@ -598,7 +599,14 @@ def execute_code(
|
||||||
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
duration = round(time.monotonic() - exec_start, 2)
|
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({
|
return json.dumps({
|
||||||
"status": "error",
|
"status": "error",
|
||||||
"error": str(exc),
|
"error": str(exc),
|
||||||
|
|
@ -608,19 +616,17 @@ def execute_code(
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
# Cleanup temp dir and socket
|
# Cleanup temp dir and socket
|
||||||
try:
|
if server_sock is not None:
|
||||||
server_sock.close()
|
try:
|
||||||
except Exception as e:
|
server_sock.close()
|
||||||
logger.debug("Server socket close error: %s", e)
|
except OSError as e:
|
||||||
try:
|
logger.debug("Server socket close error: %s", e)
|
||||||
import shutil
|
import shutil
|
||||||
shutil.rmtree(tmpdir, ignore_errors=True)
|
shutil.rmtree(tmpdir, ignore_errors=True)
|
||||||
except Exception as e:
|
|
||||||
logger.debug("Could not clean temp dir: %s", e, exc_info=True)
|
|
||||||
try:
|
try:
|
||||||
os.unlink(sock_path)
|
os.unlink(sock_path)
|
||||||
except OSError as e:
|
except OSError:
|
||||||
logger.debug("Could not remove socket file: %s", e, exc_info=True)
|
pass # already cleaned up or never created
|
||||||
|
|
||||||
|
|
||||||
def _kill_process_group(proc, escalate: bool = False):
|
def _kill_process_group(proc, escalate: bool = False):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue