Repository navigation
Description
What happens
Two symptoms in AgentProtocolTaskEventBus, the bus behind GET /tasks/{taskId}/events:
-
A channel is retained for every task id ever subscribed to.
subscribe()creates a channel withcomputeIfAbsentfor 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 incomplete(), 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. -
A late subscriber never terminates.
complete()removes the channel and then completes its sink. A client that reconnects afterwards (withfrom_seq, which the controller explicitly supports) builds a new empty channel whosefilter(seq > fromSeq)drops everything; nothing ever completes that channel, so the stream stays open until the controller'stake(timeout)fires — i.e.sseTimeoutMs=10_800_000ms (3 hours). The class javadoc promises that the replay buffer lets "late subscribers / reconnects catch up fromfromSeq", 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