Skip to content

[Bug]: EventConsumer.consumeAll busy-spins after an interrupt and pins virtual-thread carriers #1345

Closed

Description

@rmlopes

What happened?

EventConsumer.consumeAll busy-spins after its thread is interrupted, and on virtual threads this pins the carrier threads.

Version: a2a-java-sdk-server-common 1.4.0.Final

If the thread that runs EventConsumer.consumeAll is interrupted while a task stays open (for example in INPUT_REQUIRED), the consumer loop never parks again and uses 100% of a CPU until the queue closes:

  • EventQueue.ChildQueue.dequeueEventItem(int) catches the InterruptedException from queue.poll(waitMilliSeconds, MILLISECONDS), sets the interrupt flag again (Thread.currentThread().interrupt()) and returns null.
  • consumeAll reads null as a normal poll timeout and loops. For a task in an interrupted state (INPUT_REQUIRED, AUTH_REQUIRED) it keeps polling by design.
  • The flag is still set, so the next poll throws at once, and the cycle repeats with no wait.

Impact: on a virtual thread the spinning consumer never yields, so it keeps its carrier thread. When the number of such consumers reaches the carrier count, no other virtual thread in the JVM can run (HTTP handlers, agent work). We hit this after closing the executor that runs the consumers with shutdownNow() while some tasks were still open; on a 2-CPU machine the whole process stalled.

Reproduction

  1. Start a task whose agent leaves it in INPUT_REQUIRED, so its event consumer keeps polling.
  2. Interrupt the consumer's thread (for example shutdownNow() on the executor that runs it).
  3. The thread stays RUNNABLE in ChildQueue.dequeueEventItem → InterruptedException.<init>, and one core is at 100%. With more such tasks than carrier threads, a new Thread.startVirtualThread(...) never runs.

Expected: an interrupt ends the consumer, or at least does not turn into a busy loop. For example:

  • dequeueEventItem throws (EventQueueClosedException or a dedicated exception) instead of returning null on interrupt, and consumeAll stops; or
  • consumeAll checks Thread.currentThread().isInterrupted() after a null poll and exits.

Workaround: do not interrupt the threads that run event consumers (shutdown() rather than shutdownNow()), and close the task's queue to end its consumer.

Relevant log output

Thread dump of the stalled JVM (jcmd Thread.dump_to_file), 2 carrier threads, ~200% CPU:
"a2a-server-4" virtual, RUNNABLE
    java.lang.InterruptedException.<init>
    ...
    org.a2aproject.sdk.server.events.EventQueue$ChildQueue.dequeueEventItem(EventQueue.java)
    org.a2aproject.sdk.server.events.EventConsumer.consumeAll(EventConsumer.java)
"a2a-server-9" virtual, RUNNABLE (same stack)
All other virtual threads: RUNNABLE with no frames (waiting for a carrier).

Code of Conduct

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

Activity

  1. added a commit that references this issue on Nov 22, 2026
    36d7998
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

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions