Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix: restart sandbox when error occured

zhaozixuanb38150edf920
diff --git a/jiuwenbox/src/jiuwenbox/server/runtime/process.py b/jiuwenbox/src/jiuwenbox/server/runtime/process.py
index 8a489c66c7..bce4761389 100644
--- a/jiuwenbox/src/jiuwenbox/server/runtime/process.py
+++ b/jiuwenbox/src/jiuwenbox/server/runtime/process.py
@@ -166,6 +166,11 @@ DAEMON_STARTUP_LOG_MAX_BYTES = 16 * 1024
# is just an open/read/write or scandir call. Cap the IPC roundtrip at a
# short upper bound so a wedged daemon does not stall HTTP requests.
DAEMON_FILE_OP_TIMEOUT_SECONDS = 30.0
+# When the caller omits an exec timeout the daemon still waits for the
+# child to finish before responding. Cap the IPC read at the common
+# agent-core default (300s) plus the same +5s cushion used for explicit
+# timeouts so a wedged daemon cannot hang HTTP requests forever.
+DEFAULT_EXEC_IPC_READ_TIMEOUT_SECONDS = 305.0
# OS-level errors that mean the daemon is actually gone or its control
# socket is permanently broken. When we see one of these, ``_daemon_socket_ready``
@@ -2226,12 +2231,12 @@ class ProcessRuntime(RuntimeAdapter):
try:
# The daemon waits for the user command to finish before
# responding, so the receive timeout has to outlive the request
- # timeout. ``None`` means "wait forever" which matches the
- # legacy bwrap path when no timeout is configured.
+ # timeout. When no exec timeout is configured, use a bounded
+ # default instead of waiting forever on the socket.
if call.timeout is not None:
sock.settimeout(call.timeout + 5.0)
else:
- sock.settimeout(None)
+ sock.settimeout(DEFAULT_EXEC_IPC_READ_TIMEOUT_SECONDS)
self._send_request_blob(sock, header_blob, call.stdin_bytes)
try:
sock.shutdown(socket.SHUT_WR)
diff --git a/jiuwenbox/src/jiuwenbox/server/sandbox_manager.py b/jiuwenbox/src/jiuwenbox/server/sandbox_manager.py
index 7e0ad13809..514965dfd7 100644
--- a/jiuwenbox/src/jiuwenbox/server/sandbox_manager.py
+++ b/jiuwenbox/src/jiuwenbox/server/sandbox_manager.py
@@ -42,6 +42,19 @@ def _truncate_for_audit(text: str | None, *, limit: int = _AUDIT_OUTPUT_LIMIT) -
return text
return text[-limit:] + f"\n[truncated, total {len(text)} chars]"
+
+def _is_daemon_ipc_exec_failure(result: ExecResult) -> bool:
+ stderr = result.stderr or ""
+ if result.exit_code == 124 and "daemon IPC timeout" in stderr:
+ return True
+ return "daemon IPC channel unavailable" in stderr
+
+
+def _is_daemon_ipc_file_op_failure(result: RuntimeFileOpResult) -> bool:
+ if result.ok:
+ return False
+ return result.error in ("daemon_unavailable", "transport_failure")
+
from jiuwenbox.logging_config import configure_logging
from jiuwenbox.models.common import AuditEventType
from jiuwenbox.models.policy import SecurityPolicy, TimeoutPolicy
@@ -67,6 +80,7 @@ from jiuwenbox.server.runtime.base import (
RuntimeAdapter,
RuntimeBackgroundExecRequest,
RuntimeExecRequest,
+ RuntimeFileOpResult,
)
from jiuwenbox.server.runtime.process import BackgroundJobNotFoundError, ProcessRuntime
from jiuwenbox.server.workspace import JIUWENBOX_HOME
@@ -689,17 +703,24 @@ class SandboxManager:
# pre-call ``EXEC_COMMAND`` was dropped: it doubled the JSONL
# volume without adding any information not already present here.
start = time.monotonic()
+ runtime_request = RuntimeExecRequest(
+ command=request.command,
+ workdir=request.workdir,
+ env=request.env,
+ stdin_data=request.stdin_data,
+ timeout=request.timeout,
+ )
try:
- result = await self.runtime.exec(
- sandbox_id,
- RuntimeExecRequest(
- command=request.command,
- workdir=request.workdir,
- env=request.env,
- stdin_data=request.stdin_data,
- timeout=request.timeout,
- ),
- )
+ result = await self.runtime.exec(sandbox_id, runtime_request)
+ if _is_daemon_ipc_exec_failure(result):
+ logger.warning(
+ "Daemon IPC exec failed for sandbox %s (exit=%s), "
+ "restarting sandbox once",
+ sandbox_id,
+ result.exit_code,
+ )
+ await self.restart_sandbox(sandbox_id)
+ result = await self.runtime.exec(sandbox_id, runtime_request)
except Exception as exc:
self.audit.log(
AuditEventType.EXEC_COMMAND,
@@ -1028,6 +1049,14 @@ class SandboxManager:
except Exception as exc:
_emit_result(False, error=repr(exc), path="ipc")
raise
+ if not result.ok and _is_daemon_ipc_file_op_failure(result):
+ logger.warning(
+ "Daemon IPC read failed for sandbox %s (%s), restarting sandbox once",
+ sandbox_id,
+ result.error,
+ )
+ await self.restart_sandbox(sandbox_id)
+ result = await self.runtime.read_file(sandbox_id, sandbox_path)
if result.ok:
content = result.content or b""
_emit_result(True, size=len(content), path="ipc")