[Bug]: Resource leak (Ghost Task) in AgentBase#createEventStream when Flux stream is cancelled
Describe the bug
When streaming agent responses (e.g. via SSE), if the downstream consumer (client) cancels the Flux subscription, the upstream Mono executing the agent logic and LLM requests is not properly terminated.
The root cause is that in AgentBase.java (createEventStream method), the Disposable returned by Mono.defer().subscribe(...) is never registered with the FluxSink dispose lifecycle. As a result, when the downstream cancels the Flux, the LLM request continues running in the background as a "ghost task".
This leads to:
-
Resource leak
- Wasted API tokens
- Unnecessary network bandwidth usage
- Thread pool/resource exhaustion
- HTTP streaming clients continuing to receive chunks even after disconnect
-
Lock blocking
- The
Agent remains locked (running = true) until the background task naturally completes
- Subsequent requests against the same agent instance may fail with:
IllegalStateException: Agent is still running, please wait for it to finish
To Reproduce
Steps to reproduce the behavior:
- Set up an AgentScope application with a reactive web framework (e.g. Spring WebFlux) exposing an SSE endpoint.
- Send a request that triggers a long-running LLM generation.
- After streaming starts, immediately cancel the client request (e.g. close the browser tab or abort the HTTP request).
- Observe backend logs.
The underlying LLM client (e.g. DashScopeHttpClient) continues printing logs such as:
even though the downstream client has already disconnected.
Expected behavior
Cancellation from the downstream subscriber should propagate upstream.
When the Flux is cancelled:
- the background
Mono execution should be disposed immediately
- ongoing LLM network requests should stop
- the agent execution lock should be released (
running.set(false))
Current problematic code
// AgentBase.java (v1.0.12, around line 789)
sink.complete();
},
sink::error);
},
FluxSink.OverflowStrategy.BUFFER)
Proposed Fix
Register the Disposable using sink.onDispose(...) so downstream cancellation propagates to the upstream subscription.
Disposable disposable =
Mono.defer(() -> {
...
})
.subscribe(
unused -> {},
sink::error,
sink::complete);
sink.onDispose(disposable);
System Information
- AgentScope-Java Version:
v1.0.12
- Environment: Reactive Web Streaming (Spring WebFlux / SSE)
- Component:
agentscope-core
- Affected Class:
io.agentscope.core.agent.AgentBase
[Bug]: Resource leak (Ghost Task) in
AgentBase#createEventStreamwhenFluxstream is cancelledDescribe the bug
When streaming agent responses (e.g. via SSE), if the downstream consumer (client) cancels the
Fluxsubscription, the upstreamMonoexecuting the agent logic and LLM requests is not properly terminated.The root cause is that in
AgentBase.java(createEventStreammethod), theDisposablereturned byMono.defer().subscribe(...)is never registered with theFluxSinkdispose lifecycle. As a result, when the downstream cancels theFlux, the LLM request continues running in the background as a "ghost task".This leads to:
Resource leak
Lock blocking
Agentremains locked (running = true) until the background task naturally completesTo Reproduce
Steps to reproduce the behavior:
The underlying LLM client (e.g.
DashScopeHttpClient) continues printing logs such as:even though the downstream client has already disconnected.
Expected behavior
Cancellation from the downstream subscriber should propagate upstream.
When the
Fluxis cancelled:Monoexecution should be disposed immediatelyrunning.set(false))Current problematic code
Proposed Fix
Register the
Disposableusingsink.onDispose(...)so downstream cancellation propagates to the upstream subscription.System Information
v1.0.12agentscope-coreio.agentscope.core.agent.AgentBase