agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[sandbox-e2b] exec timeout is unreliable: readTimeout preempts callTimeout, no server-side kill, cancellation misreported

Open chcodex opened this issue 2 months ago • 6 comments

Problem

E2bEnvdProcessClient.runShellCapture() timeout handling (agentscope-extensions/agentscope-extensions-sandbox/agentscope-extensions-sandbox-e2b/src/main/java/io/agentscope/extensions/sandbox/e2b/E2bEnvdProcessClient.java) has three defects:

  1. Premature false timeout. The per-call client only sets callTimeout(timeoutSeconds) and inherits the base readTimeout:
http.newBuilder().callTimeout(timeoutSeconds, TimeUnit.SECONDS).build()

If base readTimeout < timeout (e.g. custom client readTimeout=1s, exec timeout=3s), the idle readTimeout fires first. The resulting InterruptedIOException is caught and wrapped as ExecTimeoutException("timed out after 3s"), but only ~1s elapsed.

  1. Timed-out remote process is left running. buildEnvdRequest() sends no Connect-Timeout-Ms header, so timeout only disconnects the client HTTP stream. The sandbox process (e.g. sleep 1000) keeps running — leak / unexpected billing.

  2. External cancellation misreported as timeout. catch (InterruptedIOException) unconditionally throws ExecTimeoutException, even when the thread was interrupted by outer cancellation. The interrupt bit is swallowed.

Repro

  • Base client readTimeout=1s, runShell(..., timeoutSeconds=3) against a hanging envd stream (NO_RESPONSE).
  • Expected: ExecTimeoutException after ~3s.
  • Actual: ExecTimeoutException after ~1s with message claiming 3s.

Expected

  • readTimeout for the call must stay above callTimeout so total-duration semantics wins.
  • Send Connect-Timeout-Ms when timeoutSeconds > 0 so envd kills the process server-side.
  • If Thread.currentThread().isInterrupted(), restore the bit and rethrow instead of wrapping as timeout.

chcodex avatar Sep 04 '26 02:09 chcodex

I traced the three failure modes to separate timeout layers and agree they should be fixed together rather than by increasing the default read timeout globally.

A useful implementation boundary would be to derive a per-call client with readTimeout >= callTimeout (while preserving a caller-configured larger read timeout), then send Connect-Timeout-Ms only for positive execution timeouts. The exception mapping should distinguish an interrupted caller from an elapsed deadline: restore the interrupt flag and propagate cancellation when the thread is interrupted; wrap only an actual deadline expiry as ExecTimeoutException. Please also add tests for readTimeout < timeout, readTimeout > timeout, zero/unbounded timeout, request-header propagation, and interrupted execution. The tests should assert elapsed behavior and the exact cancellation/timeout type, since a message claiming a timeout is not sufficient evidence.

CryoThrust avatar Sep 04 '26 13:09 CryoThrust

Issue Evaluation

Category: bug | Status: Confirmed | Severity: medium

Verified against main @ 39cd304a. Both halves of the report reproduce in the code as written.

Root cause

  • E2bEnvdProcessClient.java:70-74 — the base envd client is built with connectTimeout(30s) and readTimeout(opt.getReadTimeoutSeconds()), defaulting to 120s (E2bSandboxClientOptions.java:41). No callTimeout on the base client.
  • E2bEnvdProcessClient.java:122-125 — each exec derives http.newBuilder().callTimeout(timeoutSeconds, …), and newBuilder() inherits the 120s readTimeout. So every exec carries both the requested call timeout and a 120s per-read gap timeout.
  • readTimeout fires on the gap between received frames of the Connect server-streaming response, so for any quiet command it wins regardless of the requested budget. Effective timeout is min(readTimeout, execTimeout).
  • E2bEnvdProcessClient.java:156-158 — the resulting SocketTimeoutException (an InterruptedIOException) is caught and reported as ExecTimeoutException(shellCommand, timeoutSeconds), i.e. it claims the requested timeout elapsed when it did not.
  • No server-side bound: buildStartRequest (:236-250) sends only cmd/args/cwd/stdin — no timeout field to envd — and on abort the client simply throws. The only kill path in the module is whole-sandbox platform.killSandbox in E2bSandbox.java:71-79, which is shutdown() and is never invoked on exec timeout, so the command keeps running until sandbox TTL.
  • In-repo exposure: workspace persist/hydrate uses TAR_TIMEOUT_SECONDS = 300 (E2bSandbox.java:44,107,141), and caller-facing timeouts reach 600s (codingagent/SessionsTool.java:66) — both above the 120s default read timeout.

Impact: any exec that stays silent for over 120s (builds, installs, sleep, a stalled tar) is cut short with a misleading "timed out after Ns" message, and the orphaned process keeps consuming CPU/memory, may keep mutating the workspace, and can hold locks while the agent retries. Default 300s/600s timeouts are unreliable out of the box.

Fix direction

  1. In runShellCapture, derive the per-call client so the read timeout cannot preempt the requested one — readTimeout(max(opt.getReadTimeoutSeconds(), timeoutSeconds)), or readTimeout(0) and rely on callTimeout for streaming execs.
  2. Add a server-side bound so an aborted command does not leak: prefix the command with timeout <N> (it already runs via /bin/bash -l -c), or call envd's process-kill RPC after a client-side abort.
  3. Distinguish a read-timeout abort from genuine call-timeout expiry in the ExecTimeoutException message, so the reported number is truthful.

Point 1 is small and safe on its own; points 2 and 3 are what actually stop the leaked-process symptom. Reply /approve if you want a fix proposal generated (we would start with 1 + 3 and treat 2 as a follow-up, since a shell timeout prefix assumes timeout(1) exists in the sandbox image).


Automated evaluation by github-manager-bot

oss-maintainer avatar Sep 13 '26 13:09 oss-maintainer

Status: 72 h feedback window expired — no automated PR will be generated, this proposal window here is closed

The evaluation above went 72 hours without an /approve, /revise or /reject, so per our workflow the window is closed and no competing PR will be generated. No action is needed from anyone.

The root cause in the evaluation still stands, and the fix is already in flight from the reporter:

  • #2976 (open, non-draft) — fix(sandbox-e2b): reliable exec timeout with server-side kill, by @chcodex, closes this issue.

That is the same seam the evaluation pointed at: per-call readTimeout preempting the requested callTimeout (E2bEnvdProcessClient.java:70-74, :122-125), the missing server-side bound on an aborted exec, and the resulting ExecTimeoutException reporting a timeout that did not actually elapse (:156-158). @CryoThrust's earlier boundary proposal on this thread — derive a per-call client with readTimeout >= callTimeout, send Connect-Timeout-Ms only for positive execution timeouts, and separate an interrupted caller from an elapsed deadline — is a useful acceptance bar for that PR; it was never turned into an /approve here, so no automated proposal was generated.

As of this check #2976 is MERGEABLE with reviewDecision=REVIEW_REQUIRED at head b64ed418e0 (check rollup SUCCESS), last updated 2026-09-04T07:40Z. What it needs next is a maintainer review, not a second implementation. #2976 stays the active path for this issue.

If maintainers would still like an independent proposal here, reply on this issue and we will re-evaluate.

Automated by github-manager-bot

oss-maintainer avatar Sep 16 '26 13:09 oss-maintainer

Hi, I'd like to work on this. All three defects are clear: (1) set readTimeout = callTimeout on the per-call client so the idle timeout cannot preempt the call timeout; (2) send Connect-Timeout-Ms so the sandbox-side process is killed on timeout; (3) distinguish external cancellation (Thread.interrupt / call cancellation) from genuine read timeouts in the InterruptedIOException handler. Tests via a mock envd server for each case. PR coming shortly.

aiyili avatar Oct 01 '26 15:10 aiyili

Hi, I'd like to work on this. All three defects are clear: (1) set readTimeout = callTimeout on the per-call client so the idle timeout cannot preempt the call timeout; (2) send Connect-Timeout-Ms so the sandbox-side process is killed on timeout; (3) distinguish external cancellation (Thread.interrupt / call cancellation) from genuine read timeouts in the InterruptedIOException handler. Tests via a mock envd server for each case. PR coming shortly.

I've already submitted my pull request; could you please review it? https://github.com/agentscope-ai/agentscope-java/pull/2976

chcodex avatar Oct 02 '26 03:10 chcodex

Following up on my claim from September — starting on this now. The three defects stand: readTimeout preempts callTimeout (fix: per-call client sets readTimeout = callTimeout), no Connect-Timeout-Ms header (sandbox process leaks on timeout), and external cancellation misreported as ExecTimeoutException. Tests via a mock envd server covering each. PR coming shortly.

aiyili avatar Oct 21 '26 02:10 aiyili