Skip to content

fix(protocol): complete SSE streams for late subscribers and bound the event-bus channel map - #3786

Open
wahllllll wants to merge 1 commit into
agentscope-ai:mainfrom
wahllllll:fix/protocol-event-bus-channel-lifecycle
Open

wahllllll wants to merge 1 commit into
agentscope-ai:mainfrom
wahllllll:fix/protocol-event-bus-channel-lifecycle

Conversation

@wahllllll

Copy link
Copy Markdown
Contributor

Problem

Fixes #3785.

AgentProtocolTaskEventBus keeps a channel for every task id ever subscribed to — including ids that do not exist — and never completes a subscriber that connects after complete():

  • subscribe() builds a channel with computeIfAbsent, and complete() is the only removal. Only real tasks ever reach complete(), so 2000 subscribes with 2000 distinct ids retain 2000 channels (each with a 256-event replay buffer) on current main.
  • complete() removes the channel and then completes its sink, so a reconnect afterwards creates an empty channel whose filter(seq > fromSeq) drops everything and which nothing can ever complete → the SSE stream stays open until the controller's take(timeout), i.e. sseTimeoutMs = 3 hours by default.

Changes

before after
complete() removes the channel marks it completed and keeps it, so a late subscriber replays the buffer and then completes
channel map unbounded bounded by DEFAULT_MAX_CHANNELS = 1024; eviction prefers completed channels (oldest first) and only falls back to the oldest live channel when every channel is live
a reconnect to a finished task always opens a new channel finished ids are remembered (4× the bound), so a reconnect after eviction still returns an immediately-terminating stream
publish() and subscribe() each called computeIfAbsent both go through one bounded channel() lookup

AgentProtocolEventBus is unchanged and the existing constructors keep their behaviour; the two-argument constructor and the package-visible channelCount() exist for the tests.

Verification

Probe compiled against both versions (public API plus the private channel map), on current main (16cb024ae):

case main this branch
subscribe after complete() IllegalStateException: Timeout on blocking read for 2000000000 NANOSECONDS (2 s probe; 3 h in production) replays the 2 buffered events, then completes
2000 subscribes with distinct ids retained channels must be bounded, was 2000 ≤ 1024
  • mvn -pl agentscope-extensions/agentscope-extensions-protocol/agentscope-extensions-agent-protocol test: 37 tests, 0 failures. AgentProtocolTaskEventBusTest goes 4 → 9 cases and the four existing cases are untouched.
  • mvn spotless:check: passes.

Not addressed here

  • An unknown id still gets a channel on its first subscribe: clients are allowed to subscribe immediately after submit, so the bus cannot distinguish "a real task that has not published its first event yet" from "an id that does not exist". Closing that needs validation in the controller or the store.
  • publish() after complete() is now dropped (debug log) instead of recreating a channel. A completed task never legitimately publishes again, and recreating the channel is exactly what leaked.
  • Retention is still up to the bound, so a long-lived bus can hold up to 1024 channels; that is intentional, and the eviction order is what keeps the newest live tasks working.

…e event-bus channel map

AgentProtocolTaskEventBus created a channel for every task id ever subscribed to and
only removed it in complete(). A client that reconnected after completion therefore
opened a fresh channel that nothing could complete and waited for the SSE timeout
(three hours by default), while an id that never belonged to a real task kept a
replay-buffered channel forever.

Retain completed channels so a late subscriber drains the replay buffer and then
completes, bound the channel map (completed channels evicted first, oldest first),
and remember finished ids so a reconnect to an evicted task also terminates at once.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 重要不紧急

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants