Skip to content

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

Description

@ordercard

What happened?

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

Version: org.a2aproject.sdk:*:1.5.0.Final (also present in main as of 2026-11-09)
Environment: macOS / OpenJDK 17.0.20.1, plain Java (no CDI), InMemoryTaskStore wrapped with a
fixed per-save delay, InMemoryQueueManager, MainEventBusProcessor, handler built via
DefaultRequestHandler.builder()
Affects: non-streaming SendMessage with returnImmediately omitted or false, when the last
event is INPUT_REQUIRED or AUTH_REQUIRED

Summary

For a blocking SendMessage, the returned Task is read from TaskStore
(DefaultRequestHandler.onMessageSend, Step 5, taskStore.get(taskId)), but the blocking path only
waits for the consumer future. If the agent's last event has not been persisted by
MainEventBusProcessor when Step 5 runs, the caller receives a stale state (SUBMITTED / WORKING)
even though the task is already INPUT_REQUIRED in the store a moment later.

Terminal states are protected by the awaitingFinalEvent machinery; interrupted states are not,
because TaskState.INPUT_REQUIRED.isFinal() == false and isInterrupted() == true.

This is the interrupted-state analogue of #1247 (fixed for terminal events by #1227).

Why it happens

  1. EventQueue.MainQueue.enqueueItemInternal only calls ChildQueue.expectFinalEvent() when
    isFinalEvent(event) is true. isFinalEvent accepts TaskState.isFinal() and A2AError —
    INPUT_REQUIRED/AUTH_REQUIRED are excluded.
  2. The blocking path (DefaultRequestHandler.onMessageSend) closes the caller's ChildQueue
    (Step 2) and waits for consumption (Step 3). With isAwaitingFinalEvent() == false, the
    consumer exits as soon as the queue is closed and empty
    (ChildQueue.dequeueEventItem).
  3. Step 5 then reads TaskStore. Events still in MainQueue — acquired on the semaphore in
    enqueueItemInternal, released only after persist + distribute — are invisible at this point.
  4. MainEventBusProcessor is asynchronous and persists before distributing, so the event is
    durable shortly afterwards; the caller already returned.

The consumer's dequeueEventItem(QUEUE_WAIT_MILLISECONDS = 500) gives an accidental grace window:
with a fast store the event lands before the poll times out, which is why this is invisible in
simple examples and only shows up when persistence lags (slow DB, many events, write backlog)
beyond ~500 ms after the agent returns.

Reproduction

Self-contained single file, attached as InputRequiredStaleRepro.java. Only
a2a-java-sdk-server-common + a2a-java-sdk-spec 1.5.0.Final are required.

# pom: the two artifacts above at 1.5.0.Final
mvn -q dependency:build-classpath -Dmdep.outputFile=cp.txt

# interrupted last event, 600ms per persist -> stale
java -DsaveDelay=600 -Dcalls=3 -cp "$(cat cp.txt)" InputRequiredStaleRepro.java blocking

# same store, terminal last event -> correct
java -DsaveDelay=600 -Dcalls=3 -cp "$(cat cp.txt)" InputRequiredStaleRepro.java terminal

Observed:

mode=blocking  expected=TASK_STATE_INPUT_REQUIRED  saveDelay=600ms  artifacts=0  calls=3
  call#1  returned=TASK_STATE_SUBMITTED eventual=TASK_STATE_INPUT_REQUIRED STALE
  call#2  returned=TASK_STATE_SUBMITTED eventual=TASK_STATE_INPUT_REQUIRED STALE
  call#3  returned=TASK_STATE_SUBMITTED eventual=TASK_STATE_INPUT_REQUIRED STALE
  => 0/3 returned the expected state

mode=terminal  expected=TASK_STATE_COMPLETED  saveDelay=600ms  artifacts=0  calls=3
  call#1  returned=TASK_STATE_COMPLETED eventual=TASK_STATE_COMPLETED OK
  call#2  returned=TASK_STATE_COMPLETED eventual=TASK_STATE_COMPLETED OK
  call#3  returned=TASK_STATE_COMPLETED eventual=TASK_STATE_COMPLETED OK
  => 3/3 returned the expected state

A less artificial shape reproduces too — 50 ms per save with 12 artifact events before
requiresInput() (≈650 ms of persistence lag for the tail):

java -DsaveDelay=50 -Dartifacts=12 -Dcalls=3 -cp "$(cat cp.txt)" InputRequiredStaleRepro.java blocking
#   => 0/3, e.g. returned=TASK_STATE_WORKING / SUBMITTED, eventual=INPUT_REQUIRED

GetTask immediately afterwards returns INPUT_REQUIRED — the state is not lost, only the blocking
response is wrong.

Suggested fix

The blocking path should guarantee the same invariant the terminal path gets from
awaitingFinalEvent: no event enqueued by the agent may still be in flight in MainQueue when
Step 5 reads TaskStore.

Option A (general): expose the existing in-flight count and wait for it to reach zero before Step 2,
e.g. QueueManager.awaitDrained(taskId, timeout) implemented on MainQueue
(size() == getQueueSize() - semaphore.availablePermits()), signalled from
releaseSemaphore() — which runs in MainEventBusProcessor's finally block, after persist and
distribute. A callback-based wake-up would miss the last event, so the signal has to be on the
release path.

Option B (narrower): extend the awaitingFinalEvent accounting to
state.isFinal() || state.isInterrupted(). This fixes the status-event case, but an artifact tail
(the last event being an artifact) would still race, so Option A is preferable.

Workaround for applications

Wrap the AgentExecutor so that execute() does not return until the task's queue is drained
(poll QueueManager.get(taskId).size() with a bounded timeout), or use
returnImmediately=true plus client-side GetTask polling.

Related

Relevant log output

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added theissue type on Nov 9, 2026
  2. changed the title [-][Bug]:[/-] [+][Bug]:Blocking SendMessage returns stale task state when the agent ends in an interrupted state (INPUT_REQUIRED / AUTH_REQUIRED)[/+] on Nov 9, 2026
  3. WMK666666 commented on Nov 10, 2026

    @WMK666666
    Contributor

    Fixed in #1315.

    Root cause I confirmed while reading the blocking path in DefaultRequestHandler#onMessageSend:

    • Step 2 closes the queue, Step 3 waits until the consumers have seen queueClosed.
    • But a consumer's onEvent returns as soon as it has accepted the event; the actual persistence happens later in MainEventBusProcessor#processEvent, and only then is the semaphore permit released.
    • So when the run ends in a non-final state (input-required / auth-required), Step 5 can read the TaskStore before the last TaskStatusUpdateEvent has been written, and returns the previous state instead.

    I took your Option A rather than a fixed sleep: added EventQueue#awaitDrained(timeout, unit), which polls MainQueue.size() (i.e. getQueueSize() - semaphore.availablePermits(), the number of events accepted but not yet processed) until it reaches zero, and called it between Step 3 and Step 5 for the interrupted branch only. Terminal states were already covered by awaitingFinalEvent, so they are unaffected.

    Tests: EventQueueDrainTest (4) for the queue primitive, BlockingSendMessageStaleStateTest (3) covering input-required, auth-required and completed. Reverting the new step makes exactly the two interrupted-state tests fail (working returned instead of input-required / auth-required), while the completed one stays green — which matches the 2/3 ratio in your report.

  4. Shay-Deng commented on Nov 12, 2026

    @Shay-Deng

    I read #1314 and #1315 side by side, since both take Option A from the issue. Notes in case they help pick one. This is from the diffs and their CI (green on both); I haven't run either locally.

    Common to both: the wait sits between Step 3 and the TaskStore read, it uses the in-flight count MainQueue already has (getQueueSize() - availablePermits(), released in MainEventBusProcessor's finally after persist and distribute), ChildQueue delegates to its parent, the timeout reuses consumptionCompletionTimeoutSeconds, and a timeout falls back to today's behavior. Both also replace the Step 4 note from #611 that deliberately avoided an explicit wait here, so that is probably the main thing to sign off on.

    Differences:

    1. API. fix(server): return the interrupted state from a blocking SendMessage #1315 adds public abstract boolean awaitDrained(...) to EventQueue. EventQueue has protected constructors, so a subclass outside this repo would stop compiling. fix(server): wait for in-flight events before reading the task in blocking sendMessage #1314's awaitInFlightEventsProcessed has a no-op default and is overridden in MainQueue and ChildQueue.
    2. How it waits. fix(server): wait for in-flight events before reading the task in blocking sendMessage #1314 waits on a monitor that releaseSemaphore() notifies and checks size() while holding the same lock, so the last release can't be missed; it also routes the mainEventBus.submit failure path through releaseSemaphore(). fix(server): return the interrupted state from a blocking SendMessage #1315 polls size() every 1 ms, which is simpler and can't miss it either. Both look correct. The remark in fix(server): return the interrupted state from a blocking SendMessage #1315 that a release-path signal "would still race" doesn't apply to fix(server): wait for in-flight events before reading the task in blocking sendMessage #1314's version.
    3. When it waits. fix(server): wait for in-flight events before reading the task in blocking sendMessage #1314 waits only if the agent finished within Step 1. fix(server): return the interrupted state from a blocking SendMessage #1315 always waits. When the agent is still running after agentCompletionTimeoutSeconds, the response is documented as a snapshot; with fix(server): return the interrupted state from a blocking SendMessage #1315, a busy event bus can add up to consumptionCompletionTimeoutSeconds to that response and log a warning.
    4. Logging. On timeout fix(server): return the interrupted state from a blocking SendMessage #1315 logs a WARN twice (in MainQueue and in the handler); fix(server): wait for in-flight events before reading the task in blocking sendMessage #1314 logs one DEBUG line.
    5. Tests. fix(server): wait for in-flight events before reading the task in blocking sendMessage #1314 covers input-required, auth-required and an artifact as the last event. fix(server): return the interrupted state from a blocking SendMessage #1315 covers input-required and auth-required, and checks that completed is unchanged. Both descriptions mention the artifact tail, but only fix(server): wait for in-flight events before reading the task in blocking sendMessage #1314 tests it. A "completed is unchanged" case would be easy to add to fix(server): wait for in-flight events before reading the task in blocking sendMessage #1314.

    On balance #1314 looks a little safer to merge (no new abstract method, no extra wait while the agent is still running), and #1315's terminal-state test is worth carrying over.

  5. added a commit that references this issue on Nov 18, 2026
    f40664f
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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions