Repository navigation
Description
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 theInterruptedExceptionfromqueue.poll(waitMilliSeconds, MILLISECONDS), sets the interrupt flag again (Thread.currentThread().interrupt()) and returnsnull.consumeAllreadsnullas 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
pollthrows 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
- Start a task whose agent leaves it in
INPUT_REQUIRED, so its event consumer keeps polling. - Interrupt the consumer's thread (for example
shutdownNow()on the executor that runs it). - The thread stays
RUNNABLEinChildQueue.dequeueEventItem→InterruptedException.<init>, and one core is at 100%. With more such tasks than carrier threads, a newThread.startVirtualThread(...)never runs.
Expected: an interrupt ends the consumer, or at least does not turn into a busy loop. For example:
dequeueEventItemthrows (EventQueueClosedExceptionor a dedicated exception) instead of returningnullon interrupt, andconsumeAllstops; orconsumeAllchecksThread.currentThread().isInterrupted()after anullpoll 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
fix(server): stop interrupted event consumers from busy-spinning (a2a…