A2A Stack — Thread Safety & Correctness Cleanup
A2A Stack — Thread Safety & Correctness Cleanup
Context
The A2A server/client stack has several thread-safety issues, dead code, and protocol correctness bugs identified during integration test work. None of these are breaking for single-threaded usage but will cause failures under concurrent load or on server-side errors.
Thread Safety — TaskStateManager.kt
-
TaskInfo.events: MutableList<StreamingEventKind>— not thread-safe; concurrentrecordEventcalls can corrupt the list. Fix: replace withCopyOnWriteArrayList. -
updateStreamId()— non-atomic read-copy-write; two concurrent resubscriptions can clobber each other. Fix:ConcurrentHashMap.compute. -
var completedAt,var currentState— mutated without synchronization. Fix:@Volatileor fold into a single atomic state object. -
Apply
import javax.annotation.concurrent.ThreadSafeand see/embabel-agent/embabel-agent-api/src/test/kotlin/architecture/ThreadSafetyRulesTest.kt
Dead Code —
A2AStreamingHandler.ktprivate val scheduler: ScheduledExecutorService— allocated on startup, never used, never shut down → thread leak. Fix: remove the field and theshutdown()method.
Protocol Correctness —
AutonomyA2ARequestHandler.kt-
Line 111:
.single()on message parts — crashes on multi-part messages. Fix:.firstOrNull()?.text ?: "". -
isFinal(true)missing on error path — errorTaskStatusUpdateEventin thehandleMessageStreamcatch block lacksisFinal(true); client latch releases viaonComplete(null)→ NPE in any caller on server-side error. Fix: add.isFinal(true)to the error event builder. -
handleTasksGet/handleCancelTask:TODO()— throwsNotImplementedError→ Sonar block;tasks/getalso cannot find non-streaming tasks sincehandleMessageSendnever registers them inTaskStateManager. Fix: implementtasks/getviaTaskStateManager; register task on entry inhandleMessageSend; returnTaskNotCancelableErrorfortasks/cancel. -
Wrong error type in
handleMessageSendcatch —TaskNotFoundErrorused for internal exceptions. Fix: useInternalError. -
"Processing task…" SSE event ordering — sent after
chooseAndRunAgent()completes, not before. Fix: move before the agent call. -
// TODOcomments on lines 110, 139, 342 → Sonar fail. Fix: resolve or remove.
@hadirsa @abadongutierrez. @cpage-pivotal, @alexheifetz - looping all involved in A2A stack.
Please let me know if you have bandwidth to work on it.
I think A2A requires both cleanup and validation to align with the most recent A2A standard, and server TCK conformance testing.
I'm working on the Embabel A2A client and multi-agent context propagation; I'll release it soon.
Looping @azanux
Thanks
Hi @igordayen, I hope you're doing well. I can take this. I'll investigate and come back shortly.
To avoid conflicts:
- #2038 changes
AutonomyA2ARequestHandler. Is it close to merging? Should I branch from main and rebase later, build on top of it, or wait for it to land?
Thanks
@hadirsa -thank you very much for helping. Could you please proceed with #2038 approval/merger? Will keep you updated. thanks again
Sure, I’ll take a look and handle the review. @igordayen