Skip to content

[Bug]:HarnessAgent带沙箱运行,结束后会报错 #3415

Open

Description

@zt18661205847

2026-10-04 09:59:54.399 [session-tree-mirror] [] WARN [i.a.h.a.f.s.SandboxBackedFilesystem] - [sandbox-fs] uploadFiles failed for path: agents/GuardedAgent/sessions/session01.log.jsonl
io.agentscope.harness.agent.sandbox.SandboxException$SandboxRuntimeException: docker tar extract failed (exit=1): Error response from daemon: container ec7e8796e3efdc83fdcef7f1098ff6702d781514c1ed1d5199a5e00c3e1ad6fc is not running

at io.agentscope.harness.agent.sandbox.impl.docker.DockerSandbox.doHydrateWorkspace(DockerSandbox.java:320)
at io.agentscope.harness.agent.sandbox.AbstractBaseSandbox.hydrateWorkspace(AbstractBaseSandbox.java:186)
at io.agentscope.harness.agent.filesystem.sandbox.SandboxBackedFilesystem.uploadFiles(SandboxBackedFilesystem.java:165)
at io.agentscope.harness.agent.memory.session.SessionTree.mirrorToFilesystem(SessionTree.java:733)
at io.agentscope.harness.agent.memory.session.SessionTree.lambda$scheduleMirror$6(SessionTree.java:593)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1090)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:614)
at java.base/java.lang.Thread.run(Thread.java:1474)

2026-10-04 09:59:57.237 [boundedElastic-1] [] WARN [i.a.h.a.m.MemoryFlushMiddleware] - Memory flush failed: No active sandbox — sandbox filesystem used outside of a call context
分析原因:
HarnessAgent.wrappedCall() 使用Mono.using() 管理 sandbox 生命周期:

  1. Acquire →sandboxLifecycleMw.acquireForCall() → 启动 Docker 容器
  2. Execute → Agent 执行(LLM 推理 + 工具调用),期间:
    • SessionTree.flush() 调用scheduleMirror() 提交异步上传任务到session-tree-mirror 线程
    • MemoryFlushMiddleware.onAgent() 在doOnComplete 中调度scheduleFlush() 到boundedElastic 线程
  3. Release → 内部 Mono 完成后,Mono.using() cleanup 立即执行 :
    • filesystemProxy.clearSandboxIfCurrent() → 清除 sandbox 引用
    • sandboxManager.release() →sandbox.stop() → Docker 容器被停止
  4. 异步任务在容器停止后执行 :
    • session-tree-mirror 线程:PinnedSandboxFilesystem 持有已停止容器的引用 →hydrateWorkspace() →docker tar extract → 报错container is not running
    • boundedElastic-2 线程:MemoryFlushMiddleware.doFlush() →writeMemoryFiles() →workspaceManager.appendUtf8WorkspaceRelative() →filesystem.uploadFiles() →requireSandbox() 找不到 sandbox → 报错No active sandbox

Activity

  1. added
    bug
    on Oct 4, 2026
  2. oss-maintainer commented on Oct 4, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Status: Confirmed | Severity: medium-high (silent persistence loss + noisy WARN stack traces) | Fix: needs maintainer decision — no PR raised yet

    Both failure modes in your log reproduce from the code paths on main (a3798a8f5). Your root-cause analysis is correct; here is the precise mapping, plus one detail worth adding.

    1. session-tree-mirror → container ... is not running

    • HarnessAgent.wrappedCall (HarnessAgent.java:955-970) wraps the call in Mono.using(...), whose cleanup runs sandboxLifecycleMw.releaseForCall(...) the moment the inner Mono terminates.
    • SandboxLifecycleMiddleware.releaseForCall (:162-190) → SandboxManager.release (SandboxManager.java:184-212) calls sandbox.stop() and sandbox.shutdown() for self-managed results; Docker shutdown() removes the container.
    • SessionTree.scheduleMirror (SessionTree.java:583-597) submits the upload to a process-wide single-thread daemon executor (:94) and pins the sandbox first via pinIfSandbox → new PinnedSandboxFilesystem(sb) (:605). The pin deliberately survives clearSandboxIfCurrent (it is a no-op there, PinnedSandboxFilesystem.java:42), so the task keeps writing into a container that has already been stopped.
    • The upload then goes SandboxBackedFilesystem.uploadFiles → active.hydrateWorkspace(...) (SandboxBackedFilesystem.java:165) → AbstractBaseSandbox.hydrateWorkspace (AbstractBaseSandbox.java:186) → DockerSandbox.doHydrateWorkspace, whose exit-code check throws SandboxRuntimeException("docker tar extract failed (exit=...)") at DockerSandbox.java:334 in current main (line 320 in the v2.0.3 build in your trace) — exactly your stack trace. Note stop() only flips running to false and persists the snapshot (AbstractBaseSandbox.java:132-141); the container itself is destroyed by shutdown(), and nothing in this path consults Sandbox.isRunning() before exec'ing into it. The failure is swallowed as log.warn("[sandbox-fs] uploadFiles failed for path: ...") (SandboxBackedFilesystem.java:169).

    This is not unknown to the code — PinnedSandboxFilesystem's own javadoc says: "Self-managed sandboxes that stop on release may still fail if the async upload races past shutdown." The pin fixed the unbind race (issue #2490 / #2849) but not the container lifetime race.

    2. boundedElastic → Memory flush failed: No active sandbox

    • MemoryFlushMiddleware.onAgent (:161-167) schedules the flush from doOnComplete, i.e. after the inner source completes but before Mono.using cleanup runs; runFlush (:212-219) hops to Schedulers.boundedElastic(), so it almost always executes after release.
    • By then releaseForCall has both nulled the per-call binding (ctx.put(SandboxAcquireResult.class, null)) and cleared the proxy via clearSandboxIfCurrent (:175), so requireSandbox finds neither and throws SandboxConfigurationException("No active sandbox — sandbox filesystem used outside of a call context") (SandboxBackedFilesystem.java:239-252).
    • Path: doFlush → MemoryFlushManager.writeMemoryFiles (:225-239) → WorkspaceManager.appendUtf8WorkspaceRelative (:370) → filesystem.uploadFiles → requireSandbox. Logged only as log.warn("Memory flush failed: {}", ...) (MemoryFlushMiddleware.java:219,283).

    Impact: the session mirror (agents/<agent>/sessions/<session>.log.jsonl) and the extracted long-term memory (memory/*.md) are the write-behind records of a call. When they fail, the conversation answer is returned normally, but the transcript/memory is not persisted to the sandbox workspace — a silent data-loss surface, not just log noise. Under IsolationScope.AGENT/GLOBAL the slot is shared, so a later session resuming it would see the gap too (inference from the isolation-key model in SandboxIsolationKey, not reproduced here).

    Duplicates / related (open): this combines two already-reported gaps — #3110 (memory flush after sandbox close, same No active sandbox message) and #3036 (SessionTree mirror outliving the sandbox), both traced back to the per-call binding work in #2490/#2849.

    Fix directions — needs a maintainer call, so we are not auto-generating a PR:

    1. Drain before release — call SessionTree.awaitMirrorQuiescence(...) and MemoryBackgroundTasks.awaitQuiescence(...) in releaseForCall before sandboxManager.release(...). Both helpers already exist but are wired only into HarnessAgent.close() (:455-470), not into the per-call path. Needs a bounded timeout, and because MIRROR_EXECUTOR is a static process-wide single thread, a per-call drain couples unrelated sessions together (call N waits on call M's uploads) — that trade-off is the real design question.
    2. Lifetime-aware pin — have PinnedSandboxFilesystem / SandboxBackedFilesystem.uploadFiles check Sandbox.isRunning() (Sandbox.java:55, AbstractBaseSandbox.java:161) and, when the sandbox is already stopped, route the payload to a durable fallback (host workspace / TranscriptStore) instead of docker exec, or drop it with an explicit counter rather than a WARN stack trace.
    3. Release ordering — stop draining after the release (per-call) vs. keep the sandbox running until in-flight writes finish (e.g. ref-counted release). Most correct, biggest change to the lifecycle contract.

    Also worth noting: runFlush reads the per-call RuntimeContext after the call is over, so even fixing the sandbox lifetime leaves the flush depending on state that releaseForCall mutates (issue #2849 describes the same class of residual race). Whatever direction is chosen should cover both callers, not only the mirror.

    Repro conditions from your report (Docker sandbox, self-managed lifecycle, GuardedAgent with session logging + memory flush) match the code path, so this is reproducible without extra information. If you can share whether the session/memory files are missing on disk afterwards (vs. only the WARN), that would confirm the impact scope for item 2 above.


    Automated evaluation by github-manager-bot

  3. added
    area/harness
    on Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/harnessbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions