Skip to content

[Bug]:SubAgentTool leaks orphan ReActAgent on timeout with retry #1783

Closed

Description

@xiaguanglei

Describe the bug

When ToolExecutor times out a SubAgentTool call and triggers a retry
(maxAttempts > 1), the old ReActAgent instance continues running to
completion in the background. The framework has no mechanism to interrupt or
cancel the orphaned agent, causing wasted LLM tokens, SSH connections, and
thread pool resources.

To Reproduce

Test code

Below reproduces the bug on the current code (without any fix).

// ==== slow tool: sleeps 4s, writes 1 line to file ====
AgentTool slowTool = new AgentTool() {
    @Override public String getName() { return "slow_tool"; }
    @Override public Map<String, Object> getParameters() { return Map.of(); }
    @Override public String getDescription() { return "slow"; }
    @Override
    public Mono<ToolResultBlock> callAsync(ToolCallParam p) {
        return Mono.fromRunnable(() -> {
            try { Thread.sleep(4_000); Files.writeString(tmpFile, "done\n"); }
            catch (Exception ignored) {}
        }).subscribeOn(Schedulers.boundedElastic())
          .then(Mono.just(ToolResultBlock.of(TextBlock.of("result"))));
    }
};

// ==== slow agent: model returns tool call ====
MockModel slowModel = MockModel.withToolCall("slow_tool", "c1", Map.of());
ReActAgent slowAgent = ReActAgent.builder()
    .name("slow").sysPrompt("slow").model(slowModel).toolkit(slowTk).build();

// ==== fast agent: returns text immediately ====
ReActAgent fastAgent = ReActAgent.builder()
    .name("fast").sysPrompt("fast").model(new MockModel("OK")).toolkit(new Toolkit()).build();

// ==== SubAgentTool with 3s timeout + 1 retry ====
// provider: 1st → slowAgent, 2nd (retry) → fastAgent
SubAgentTool tool = new SubAgentTool(provider, config);

tool.callAsync(param)
    .timeout(Duration.ofSeconds(3))
    .retryWhen(Retry.backoff(1, Duration.ofMillis(100)))
    .block();

How to execute

mvn test -pl agentscope-core \
  -Dtest="SubAgentToolTimeoutRetryIntegrationTest"

What happens (on current code, no fix)

  1. slowAgent starts → model returns tool call → slow_tool begins (4s sleep).
  2. After 3s, Mono.timeout() fires, retryWhen() creates a new subscription.
  3. provider.provide() → new fastAgent runs, completes immediately.
  4. mono.block() returns. But the old slowAgent is still running.
  5. At 4s, slow_tool finishes and writes "done\n" to file.
  6. The old agent's ReAct loop continues → calls slowModel.stream() again
    → another tool call → another slow_tool execution at ~8s → another
    line written to file.
  7. The loop continues indefinitely — each round: 4s sleep → write line →
    model call → tool call → 4s sleep → ...

Observable proof of the bug

  • slowModel.getCallCount() >= 2 — the old agent made multiple LLM calls
    after being timed out.
  • File has >= 2 lines — the old agent kept executing tools in the
    background long after the caller moved on.

Expected behavior

When a SubAgentTool Mono is cancelled (by timeout), the already-created
sub-agent should be interrupted via ReActAgent.interrupt(ctx) so its
ReAct loop terminates at the next checkInterrupted() checkpoint.

After the fix:

  • slowModel.getCallCount() == 1 — the old agent does NOT make a second LLM call.
  • File has exactly 1 line — slow_tool finished its in-flight execution
    but the agent loop was stopped before scheduling another one.

Production impact

A single inspection run with 9 sub-agents (maxAttempts=3, timeout=10min):

Metric Value
Actual invocations 19 (not 9) — 8 timed out + retried, 2 retried twice
Wasted tokens ~1 million
Execution time 30+ minutes (orphans contending for same resources)

Root cause

SubAgentTool.executeConversation() creates a ReActAgent via
agentProvider.provide(), wraps its execution in a Mono, but never
registers a cancellation handler. When Mono.timeout() cancels the
upstream, the agent is left orphaned:

Mono.timeout() → cancels upstream subscription
retryWhen()    → new subscription → new ReActAgent runs ✅
Old ReActAgent → unreachable, runs to completion in background ❌
→ LLM calls continue → tools execute → resources leak

Environment

  • AgentScope-Java Version: 2.0.0-SNAPSHOT
  • Java Version: 17+
  • OS: macOS / Linux
  • Relevant classes:
    • io.agentscope.core.tool.subagent.SubAgentTool
    • io.agentscope.core.tool.ToolExecutor
    • io.agentscope.core.ReActAgent

Additional context

A regression test that verifies both the bug and the fix behavior is at:
agentscope-core/src/test/.../SubAgentToolTimeoutRetryIntegrationTest.java

The test uses real ReActAgent + MockModel (no real LLM required).

Activity

  1. added
    bug
    on Jun 17, 2026
  2. added
    area/core/tool
    on Jun 17, 2026
  3. moved this from Backlog to In progress in AgentScope Javaon Jun 17, 2026
  4. moved this from In progress to Done in AgentScope Javaon Jun 17, 2026
  5. added a commit that references this issue on Jun 17, 2026
    029cc55
  6. added a commit that references this issue on Aug 12, 2026
    981c43e
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/core/toolbug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions