agentscope-ai / agentscope-ai/agentscope-java

[Bug]:Resource leak (Ghost Task) in `AgentBase#createEventStream` when `Flux` stream is cancelled

未關閉
#1,466 1 則留言 0 個 reaction 已指派 1 人 已被 @guslegend0510 認領 在 GitHub 檢視
area/core/agent bug
主要語言
Java
星號
5.6k
分支
1.3k
平均合併
4 天 12 小時
30 天內合併 PR
77

描述

## [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:

1. **Resource leak**
- Wasted API tokens
- Unnecessary network bandwidth usage
- Thread pool/resource exhaustion
- HTTP streaming clients continuing to receive chunks even after disconnect

2. **Lock blocking**
- The `Agent` remains locked (`running = true`) until the background task naturally completes
- Subsequent requests against the same agent instance may fail with:

```text
IllegalStateException: Agent is still running, please wait for it to finish
```

### To Reproduce

Steps to reproduce the behavior:

1. Set up an AgentScope application with a reactive web framework (e.g. Spring WebFlux) exposing an SSE endpoint.
2. Send a request that triggers a long-running LLM generation.
3. After streaming starts, immediately cancel the client request (e.g. close the browser tab or abort the HTTP request).
4. Observe backend logs.

The underlying LLM client (e.g. `DashScopeHttpClient`) continues printing logs such as:

```text
Received SSE data chunk
```

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

```java
// 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.

```java
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`

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。