Heuristic:A2aproject A2a java Subscriber Threshold Cleanup
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Resource_Management, Server |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Automatic ChildQueue closure when the bounded deque overflows prevents slow subscribers from consuming unbounded server memory, trading connection termination for server stability.
Description
Each client subscription to a task's event stream creates a `ChildQueue` backed by a bounded `LinkedBlockingDeque` (sized to the parent `MainQueue`'s `queueSize`, default 1000). When the `MainEventBusProcessor` distributes an event to a child via `internalEnqueueItem()`, it uses `queue.offer(item)` — a non-blocking attempt. If the offer fails (deque full), the `ChildQueue` is closed immediately (`close(true)`), clearing all pending events and signaling `EventQueueClosedException` to the consumer. This is a deliberate fail-fast strategy: rather than blocking the single-threaded `MainEventBusProcessor` (which would stall distribution to all other subscribers), the slow subscriber is disconnected.
Additionally, the `MainQueue` keeps the queue open for non-finalized tasks even when all children have closed, supporting fire-and-forget and late resubscription patterns. The `TaskStreamLifecycleHook` provides a callback mechanism for monitoring subscriber counts and implementing custom stream closure policies.
Usage
Use this heuristic when: debugging `ChildQueue is full. Closing immediately` warnings, implementing custom `TaskStreamLifecycleHook` for subscriber management, or tuning the number of concurrent SSE subscribers per task.
The Insight (Rule of Thumb)
- Action: Do not attempt to "save" full ChildQueues — the immediate close is by design. Instead, ensure clients consume events promptly.
- Value: ChildQueue capacity = `DEFAULT_QUEUE_SIZE = 1000` events. A subscriber that falls behind by more than 1000 events is disconnected.
- Trade-off: A slow client loses its event stream entirely (must re-subscribe) vs. the server risks OOM or stalls all subscribers. The design prioritizes server stability.
- Monitoring: Use `TaskStreamLifecycleHook.onSubscribe()` and `onUnsubscribe()` to track subscriber counts and alert on rapid disconnects.
- Resubscription: The `MainQueue` stays open for non-finalized tasks, so disconnected clients can re-subscribe via `subscribeTask` and resume from the current state.
Reasoning
The `MainEventBusProcessor` is single-threaded — it processes events sequentially, persisting to `TaskStore` then distributing to all child queues. If distribution to one child blocked (using `put()` instead of `offer()`), it would stall distribution to all other subscribers and block the entire event pipeline. The `offer()` + immediate-close pattern ensures the processor never blocks on distribution. The bounded deque also prevents memory exhaustion: without it, a slow SSE consumer over a flaky network could accumulate millions of events in heap. The fire-and-forget pattern (keeping `MainQueue` open when no children remain and task is not finalized) supports the A2A spec's requirement that tasks can be started without a subscriber and subscribed to later.
Code Evidence
ChildQueue bounded deque from `EventQueue.java:899-905`:
public ChildQueue(MainQueue parent) {
this.parent = parent;
// Bound the child queue with the same capacity as its parent so that a slow
// subscriber cannot grow the deque unboundedly.
this.queue = new LinkedBlockingDeque<>(parent.getQueueSize());
}
Non-blocking offer with immediate close from `EventQueue.java:933-936`:
if (!queue.offer(item)) {
LOGGER.warn("ChildQueue {} is full. Closing immediately.", this);
close(true); // immediate close
}
MainQueue stays open for non-finalized tasks from `EventQueue.java:744-752`:
// No children left - check if task is finalized before closing
if (taskStateProvider != null && taskId != null) {
boolean isFinalized = taskStateProvider.isTaskFinalized(taskId);
if (!isFinalized) {
LOGGER.debug("MainQueue for task {} has no children, but task is not finalized - keeping queue open for potential resubscriptions", taskId);
return; // Don't close - keep queue open for fire-and-forget or late subscribes
}
}
StreamCloseHandle for subscriber monitoring from `EventQueue.java:503-513`:
private final StreamCloseHandle streamCloseHandle = new StreamCloseHandle() {
@Override
public void closeStreams() {
closeChildren();
}
@Override
public int getActiveSubscriberCount() {
return getActiveChildCount();
}
};