Skip to content

[Bug]: Agent Protocol event bus keeps a channel for every unknown task id and never completes a subscriber that reconnects after completion #3785

Open

Description

@wahllllll

What happens

Two symptoms in AgentProtocolTaskEventBus, the bus behind GET /tasks/{taskId}/events:

  1. A channel is retained for every task id ever subscribed to. subscribe() creates a channel with computeIfAbsent for any id, including ids that do not exist — a typo, a stale id, or a guessed one. AgentProtocolController.events() passes the path variable straight through without checking that the task exists. The only removal is in complete(), which is called only for real tasks, so each such subscribe permanently retains a channel with its 256-event replay buffer. Measured: 2000 subscribes with 2000 distinct ids leave 2000 channels, zero reclaimed.

  2. A late subscriber never terminates. complete() removes the channel and then completes its sink. A client that reconnects afterwards (with from_seq, which the controller explicitly supports) builds a new empty channel whose filter(seq > fromSeq) drops everything; nothing ever completes that channel, so the stream stays open until the controller's take(timeout) fires — i.e. sseTimeoutMs = 10_800_000 ms (3 hours). The class javadoc promises that the replay buffer lets "late subscribers / reconnects catch up from fromSeq", which is the opposite of what happens.

Root cause

AgentProtocolTaskEventBus (current main):

// subscribe(), line 66
Channel ch = channels.computeIfAbsent(taskId, id -> new Channel(replayBufferSize));
// complete(), lines 77-79
Channel ch = channels.remove(taskId);
if (ch != null) {
    ch.sink.tryEmitComplete();
}

AgentProtocolController.events(), line 123:

return store.eventBus().subscribe(taskId, from).map(this::toSse).take(timeout);

timeout comes from AgentProtocolProperties#sseTimeoutMs, line 38:

private long sseTimeoutMs = 10_800_000L;

Reproduction

Both cases reproduce against unmodified main with a probe test that only uses the public API (plus the package-private channel count):

case result on main
subscribe after complete() IllegalStateException: Timeout on blocking read for 2000000000 NANOSECONDS — the subscriber really hangs (2 s in the probe, 3 h in production)
2000 subscribes with distinct ids retained channels must be bounded, was 2000

Suggested fix

  • retain the channel on complete() (mark it completed and complete its sink) instead of removing it, so a late subscriber replays the buffer and then terminates;
  • bound the channel map, evicting completed channels first (oldest first) and only falling back to the oldest live channel when every channel is live;
  • remember completed task ids past their channel, so a reconnect to an evicted-but-finished task also terminates immediately instead of opening a channel that can never complete.

The constructor signatures stay unchanged. I have this implemented with tests (existing 4 test cases untouched, 5 added) and can open a PR if that direction looks right.

Not addressed here

An unknown task id still gets a channel on its first subscribe: the bus cannot tell "a real task that has not published its first event yet" (clients are allowed to subscribe right after submit) from "an id that does not exist". Closing that hole needs validation in the controller or the store, and is better done separately.

Activity

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