agentscope-ai / agentscope-ai/agentscope-java

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

Aberta
#1,466 1 comentário 0 reações 1 responsável Reivindicada por @guslegend0510 Ver no GitHub
area/core/agent bug
Linguagem predominante
Java
Estrelas
5.6k
Forks
1.3k
Merge médio
4d 12h
PRs com merge (30d)
77

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.