Improve error handling and logging in code execution tool
This commit is contained in:
parent
90fa9e54ca
commit
7b1f40dd00
1 changed files with 17 additions and 14 deletions
|
|
@ -311,6 +311,7 @@ def _rpc_server_loop(
|
||||||
sys.stderr.close()
|
sys.stderr.close()
|
||||||
sys.stdout, sys.stderr = _real_stdout, _real_stderr
|
sys.stdout, sys.stderr = _real_stdout, _real_stderr
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
|
logger.error("Tool call failed in sandbox: %s", exc, exc_info=True)
|
||||||
result = json.dumps({"error": str(exc)})
|
result = json.dumps({"error": str(exc)})
|
||||||
|
|
||||||
tool_call_counter[0] += 1
|
tool_call_counter[0] += 1
|
||||||
|
|
@ -327,9 +328,9 @@ def _rpc_server_loop(
|
||||||
conn.sendall((result + "\n").encode())
|
conn.sendall((result + "\n").encode())
|
||||||
|
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
pass
|
logger.debug("RPC listener socket timeout")
|
||||||
except OSError:
|
except OSError as e:
|
||||||
pass
|
logger.debug("RPC listener socket error: %s", e, exc_info=True)
|
||||||
finally:
|
finally:
|
||||||
if conn:
|
if conn:
|
||||||
try:
|
try:
|
||||||
|
|
@ -468,8 +469,8 @@ def execute_code(
|
||||||
keep = max_bytes - total
|
keep = max_bytes - total
|
||||||
chunks.append(data[:keep])
|
chunks.append(data[:keep])
|
||||||
total += len(data)
|
total += len(data)
|
||||||
except (ValueError, OSError):
|
except (ValueError, OSError) as e:
|
||||||
pass
|
logger.debug("Error reading process output: %s", e, exc_info=True)
|
||||||
|
|
||||||
stdout_reader = threading.Thread(
|
stdout_reader = threading.Thread(
|
||||||
target=_drain, args=(proc.stdout, stdout_chunks, MAX_STDOUT_BYTES), daemon=True
|
target=_drain, args=(proc.stdout, stdout_chunks, MAX_STDOUT_BYTES), daemon=True
|
||||||
|
|
@ -547,11 +548,11 @@ def execute_code(
|
||||||
import shutil
|
import shutil
|
||||||
shutil.rmtree(tmpdir, ignore_errors=True)
|
shutil.rmtree(tmpdir, ignore_errors=True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.debug("Could not clean temp dir: %s", 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:
|
except OSError as e:
|
||||||
pass
|
logger.debug("Could not remove socket file: %s", e, exc_info=True)
|
||||||
|
|
||||||
|
|
||||||
def _kill_process_group(proc, escalate: bool = False):
|
def _kill_process_group(proc, escalate: bool = False):
|
||||||
|
|
@ -561,11 +562,12 @@ def _kill_process_group(proc, escalate: bool = False):
|
||||||
proc.terminate()
|
proc.terminate()
|
||||||
else:
|
else:
|
||||||
os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
|
os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
|
||||||
except (ProcessLookupError, PermissionError):
|
except (ProcessLookupError, PermissionError) as e:
|
||||||
|
logger.debug("Could not kill process group: %s", e, exc_info=True)
|
||||||
try:
|
try:
|
||||||
proc.kill()
|
proc.kill()
|
||||||
except Exception as e:
|
except Exception as e2:
|
||||||
logger.debug("Could not kill process: %s", e)
|
logger.debug("Could not kill process: %s", e2, exc_info=True)
|
||||||
|
|
||||||
if escalate:
|
if escalate:
|
||||||
# Give the process 5s to exit after SIGTERM, then SIGKILL
|
# Give the process 5s to exit after SIGTERM, then SIGKILL
|
||||||
|
|
@ -577,11 +579,12 @@ def _kill_process_group(proc, escalate: bool = False):
|
||||||
proc.kill()
|
proc.kill()
|
||||||
else:
|
else:
|
||||||
os.killpg(os.getpgid(proc.pid), signal.SIGKILL)
|
os.killpg(os.getpgid(proc.pid), signal.SIGKILL)
|
||||||
except (ProcessLookupError, PermissionError):
|
except (ProcessLookupError, PermissionError) as e:
|
||||||
|
logger.debug("Could not kill process group with SIGKILL: %s", e, exc_info=True)
|
||||||
try:
|
try:
|
||||||
proc.kill()
|
proc.kill()
|
||||||
except Exception as e:
|
except Exception as e2:
|
||||||
logger.debug("Could not kill process: %s", e)
|
logger.debug("Could not kill process: %s", e2, exc_info=True)
|
||||||
|
|
||||||
|
|
||||||
def _load_config() -> dict:
|
def _load_config() -> dict:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue