Skip to content

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

@philo-x

Description

@philo-x

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

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

// 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions