Repository navigation
Description
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)
slowAgentstarts → model returns tool call →slow_toolbegins (4s sleep).- After 3s,
Mono.timeout()fires,retryWhen()creates a new subscription. provider.provide()→ newfastAgentruns, completes immediately.mono.block()returns. But the oldslowAgentis still running.- At 4s,
slow_toolfinishes and writes"done\n"to file. - The old agent's ReAct loop continues → calls
slowModel.stream()again
→ another tool call → anotherslow_toolexecution at ~8s → another
line written to file. - 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_toolfinished 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.SubAgentToolio.agentscope.core.tool.ToolExecutorio.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).
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
fix(SubAgentTool): interrupt orphan sub-agent on tool call cancellati…
fix(SubAgentTool): interrupt orphan sub-agent on tool call cancellati…