Skip to content

[Bug]: Parent cancel leaves a sync-spawned subagent's event stream open (AgentStartEvent with no AgentEndEvent) #2480

Closed

Description

@gxgeek-n

Summary

AgentSpawnTool emits an AgentStartEvent unconditionally before subscribing to a sync-spawned child, then emits the matching AgentEndEvent from a doOnTerminate callback. Reactor's doOnTerminate fires on onComplete / onError but not on cancel, so cancelling the parent leaves the child's event stream open: the consumer received a start with no end and keeps rendering that subagent as running.

Affected version

2.0.0 and current main.

Code path

agentscope-harness/src/main/java/io/agentscope/harness/agent/tool/AgentSpawnTool.java, Path 1 (streamEvents() — the non-deprecated path):

parentEmitter.emit(
        new AgentStartEvent(spawned.sessionId(), null, spawned.agentId())
                .withSource(sourcePath));           // always emitted

return manager.invokeAgent(agent, sessionId, userId, prompt, parentCtx)
        .contextWrite(...)
        .doOnTerminate(                             // ← skips cancel
                () -> parentEmitter.emit(
                        new AgentEndEvent(null).withSource(sourcePath)));

Triggered by any upstream cancel: user "Stop", an outer Mono.timeout(), or Reactor dispose().

Reactor semantics, verified directly against reactor-core 3.7.6:

terminal signal doOnTerminate doFinally
complete 1 1
error 1 1
cancel 0 1

Path 2 (deprecated SubagentEventBus) relies on the child's own isLast() events and Path 3 is non-streaming, so neither is affected. doOnTerminate appears exactly once in this file.

Worth noting the same file already uses doFinally for the timeout-promotion path, and the comment there explicitly reasons about doFinally(CANCEL) — so this looks like an oversight rather than intent.

Reproduction

Mock a HarnessAgent whose call() returns Mono.never(), spawn it through agentSpawn, record events via an AgentEventEmitter in the Reactor Context, then dispose() the subscription:

AgentStartEvent count = 1
AgentEndEvent   count = 0     ← expected 1

Impact

Consumers that pair start/end events (UI progress indicators, tracing, any accounting of in-flight subagents) never see the child close. In a long-lived session every cancelled spawn leaks one permanently "running" subagent in the consumer's view.

Suggested fix

Use doFinally so all three terminal signals close the stream:

.doFinally(signal -> parentEmitter.emit(
        new AgentEndEvent(null).withSource(sourcePath)));

Relationship to #2408 / #2412

Distinct concern. #2408 / #2412 fix whether the child execution actually stops (interruptAgent being a no-op for HarnessAgent, plus the child RuntimeContext mismatch) and touch interruptAgent — I checked #2412's diff and it does not reference doOnTerminate, AgentStartEvent or AgentEndEvent. Even with the child correctly interrupted, the event stream still has to close. The two fixes are complementary and touch different lines of the same file.

PR incoming with the change plus regression coverage (cancel path fails without it; normal completion passes both ways, pinning that no duplicate events appear on the happy path). Full agentscope-harness suite: 693 tests, 0 failures.

Activity

  1. oss-maintainer commented on Jul 29, 2026

    @oss-maintainer
  2. added a commit that references this issue on Jul 29, 2026
    d6787cc
  3. added a commit that references this issue on Aug 12, 2026
    adfb6c8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions