Skip to content

fix(server): return the interrupted state from a blocking SendMessage - #1315

Merged
kabir merged 1 commit into
a2aproject:mainfrom
WMK666666:fix-blocking-send-interrupted-state
Nov 18, 2026
Merged

kabir merged 1 commit into
a2aproject:mainfrom
WMK666666:fix-blocking-send-interrupted-state

Conversation

@WMK666666

Copy link
Copy Markdown
Contributor

Problem

Fixes #1302.

A blocking SendMessage reads the task back from the TaskStore once consumption completes (DefaultRequestHandler.onMessageSend, Step 5). Completion of consumption only proves that the consumer saw the events that had already been distributed to it. The last event of an agent turn can still be inside MainEventBusProcessor at that point — it persists the event, then distributes it, and only then releases the queue permit — so Step 5 reads the state from before that event:

agent: startWork()        → SUBMITTED / WORKING persisted, distributed
agent: requiresInput()    → accepted by the queue, waiting inside MainEventBusProcessor
handler: Step 3 returns   → consumer is done, it never saw requiresInput()
handler: Step 5 reads     → WORKING          (store later says INPUT_REQUIRED)

Nothing is logged and nothing is thrown; the caller just gets a task that is one event behind, and the next GetTask shows the real state.

Terminal states are not affected: the awaitingFinalEvent machinery keeps the consumer waiting for a final event. TaskState.INPUT_REQUIRED.isFinal() == false (and isInterrupted() == true), so interrupted turns have no such protection. An artifact-only tail has the same problem.

Change

  • EventQueue#awaitDrained(timeout, unit) — waits until no event accepted by the queue is still in flight. MainQueue derives that from the semaphore it already uses for backpressure (getQueueSize() - availablePermits()); ChildQueue delegates to its parent, which is where in-flight events are tracked. The permit is released in MainEventBusProcessor.processEvent's finally block, i.e. after persist and distribute, so a drained queue means persisted.
  • DefaultRequestHandler#onMessageSend — new Step 4 between "wait for consumption" and "read the TaskStore". It reuses consumptionCompletionTimeoutSeconds (default 5s) rather than adding another knob. On timeout it logs a warning and keeps the previous behaviour instead of failing the request, so a wedged processor degrades to what callers had before.

MainQueue takes the drain permit view rather than a callback on release, because a wake-up signalled from the release path would still race with the last releaseSemaphore(); polling the permit count cannot miss it. The interval is 1 ms, and the wait only happens on the blocking path, which is already blocking on agentFuture.get(...) and consumptionFuture.get(...).

Verification

  • server-common: 620 tests pass, 7 of them new
  • New EventQueueDrainTest (4): idle queue drains immediately; an accepted-but-unprocessed event is not drained; awaitDrained actually blocks until the permit is released; a child queue reports its parent's in-flight events.
  • New BlockingSendMessageStaleStateTest (3): the task store is wrapped so that persisting a chosen state is delayed, making the ordering deterministic instead of timing-dependent.
    • agent ends in INPUT_REQUIRED → blocking send returns INPUT_REQUIRED
    • agent ends in AUTH_REQUIRED → returns AUTH_REQUIRED
    • agent ends in COMPLETED → returns COMPLETED (unchanged)
  • Reverse check — reverting only the new Step 4, leaving the tests untouched:
blockingSendReturnsInputRequiredEvenWhenTheLastEventIsStillBeingPersisted:
  expected: <TASK_STATE_INPUT_REQUIRED> but was: <TASK_STATE_WORKING>
blockingSendReturnsAuthRequiredEvenWhenTheLastEventIsStillBeingPersisted:
  expected: <TASK_STATE_AUTH_REQUIRED> but was: <TASK_STATE_WORKING>
blockingSendOfATerminalStateIsUnaffected: still passes

which is exactly the symptom reported in #1302 (returned=TASK_STATE_SUBMITTED eventual=TASK_STATE_INPUT_REQUIRED), and confirms the terminal path is untouched.

A blocking SendMessage reads the task back from the TaskStore once consumption
completes, but completion only proves the consumer saw the events that had
already been distributed to it. The last event of an agent turn can still be
inside MainEventBusProcessor at that point, which persists it *after* the
consumer is done, so the read returns the state from before that event:
SUBMITTED or WORKING instead of INPUT_REQUIRED, with nothing logged and no error
raised.

Terminal states never hit this - the awaitingFinalEvent machinery keeps the
consumer waiting for the final event. Interrupted states (INPUT_REQUIRED,
AUTH_REQUIRED) are not final, and neither is an artifact-only tail.

EventQueue#awaitDrained waits until no event accepted by the queue is left in
flight, i.e. until every semaphore permit taken at enqueue time has been
released by MainEventBusProcessor - which happens in its finally block, after
the event has been persisted and distributed. ChildQueue delegates to its
parent, since in-flight events are tracked there.

The blocking path now waits there between waiting for consumption and reading
the TaskStore. A timeout is logged and the previous behaviour is kept, so a
stuck processor degrades to what callers had before instead of failing the
request.

Fixes a2aproject#1302
@kabir
kabir merged commit f40664f into a2aproject:main Nov 18, 2026
14 checks passed
@kabir

kabir commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Thank you @WMK666666 :-)

VISHNU0906 added a commit to VISHNU0906/a2a-java that referenced this pull request Nov 22, 2026
Follow-up to a2aproject#1315 on top of current main.

- EventQueue#awaitDrained is no longer abstract. The default returns true, so
  custom EventQueue subclasses written before a2aproject#1315 keep compiling and do not
  make the blocking path wait.
- MainQueue wakes awaitDrained when a permit is released instead of polling
  every millisecond. The permit released on a failed enqueue goes through
  releaseSemaphore() too, so it also wakes waiters.
- The blocking SendMessage path only waits for the drain when the agent
  finished within agentCompletionTimeoutSeconds. A still-running agent can keep
  enqueueing, and its later events are not part of the returned task anyway.

Tests cover a trailing artifact whose persistence is slow, the skipped drain
when the agent times out, and the default implementation.

This fixes a2aproject#1302

Signed-off-by: KVR Vishnu <[email protected]>
kabir pushed a commit that referenced this pull request Nov 24, 2026
* fix(server): wait for in-flight events before reading the task in blocking sendMessage

A blocking SendMessage reads the task from the TaskStore once the
consumer stops, but only a final event keeps the consumer polling after
the handler closes its queue. When persisting the agent's last event
takes longer than the consumer's poll, an interrupted state
(INPUT_REQUIRED, AUTH_REQUIRED) or a trailing artifact was still in
flight and the call returned the previous state.

After consumption completes and the agent has returned, wait (bounded by
the consumption timeout) until the MainQueue has no events left in the
event bus. MainQueue now wakes waiters when MainEventBusProcessor
releases a permit.

Fixes #1302

Signed-off-by: KVR Vishnu <[email protected]>

* fix(server): skip the drain wait when the agent is still running

Follow-up to #1315 on top of current main.

- EventQueue#awaitDrained is no longer abstract. The default returns true, so
  custom EventQueue subclasses written before #1315 keep compiling and do not
  make the blocking path wait.
- MainQueue wakes awaitDrained when a permit is released instead of polling
  every millisecond. The permit released on a failed enqueue goes through
  releaseSemaphore() too, so it also wakes waiters.
- The blocking SendMessage path only waits for the drain when the agent
  finished within agentCompletionTimeoutSeconds. A still-running agent can keep
  enqueueing, and its later events are not part of the returned task anyway.

Tests cover a trailing artifact whose persistence is slow, the skipped drain
when the agent times out, and the default implementation.

This fixes #1302

Signed-off-by: KVR Vishnu <[email protected]>

---------

Signed-off-by: KVR Vishnu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:Blocking SendMessage returns stale task state when the agent ends in an interrupted state (INPUT_REQUIRED / AUTH_REQUIRED)

2 participants