Repository navigation
Conversation
…s lock onSubscribeToTask read the task from the store and tapped its queue in two unordered steps, while the bus thread persisted and distributed each event. An event persisted after the read and distributed before the tap reached neither the snapshot nor the stream: an artifact was lost, or a terminal status was lost and the stream never closed (A2A spec 3.1.6). MainEventBusProcessor.processEvent now holds a private monitor from persisting an event to distributing it to the child queues, and onSubscribeToTask runs its read, checks, tap and createOrTap fallback under the same monitor (withEventProcessingPaused). Adds SubscribeWindowTest. Fixes a2aproject#1370
Shay-Deng
left a comment
There was a problem hiding this comment.
Thanks for this, and for the clear reproduction in #1370. I checked it locally: mvn -pl server-common test passes on 74c8886 (665 tests). With server-common/src/main reverted to the merge base (3f321f1), both SubscribeWindowTest cases fail the way the issue describes: the artifact published in the window reaches neither the snapshot nor the stream, and the stream never closes after the terminal update. So the test pins the bug, and the change fixes it.
One question about the lock's scope. persistAndDistributeLock is one monitor per MainEventBusProcessor, which is @ApplicationScoped and processes the events of all tasks on a single thread. While a subscriber to task A runs taskStore.get(A) and the tap under the monitor, the bus can't persist or distribute an event for any other task. With the in-memory store that window is short. With JpaDatabaseTaskStore, every subscribe puts a database read on the path of every task's events. The invariant only needs ordering within one task (the read and tap of A against the persist and distribute of A). Would a monitor keyed by task ID, for example a small striped set, keep the fix and leave other tasks unaffected? I'm happy to try that variant against SubscribeWindowTest if it helps.
…e's read and tap The subscribe fix held one processor-wide monitor while a subscriber read the task and tapped its queue, and while the bus persisted and distributed any event. The ordering it protects is per task, so a subscribe on one task held up every other task's events, with a database read on that path when the TaskStore is JDBC-backed. The monitor becomes 64 lock stripes indexed by a hash of the task ID: processEvent holds the stripe of the event's task, and withEventProcessingPaused(taskId, action) holds the same stripe for the read and the tap. A fixed stripe set keeps memory bounded and needs no cleanup when a task ends; tasks that share a stripe only share contention. The Javadoc states the remaining limit (the processor is single-threaded, so an event of the held task makes later events wait) and forbids nesting the stripes of two tasks. Adds MainEventBusProcessorTaskLockTest: while a subscriber holds task A's stripe, a task-B event is persisted and distributed and a task-A event waits; a second subscriber to A blocks, one to B does not.
|
Thanks for checking this so carefully, and for running I've pushed a follow-up commit, 32e658d, that makes the lock per task:
Results with the commit ( Thanks again for offering to try the variant; if you see a case the stripes don't cover, I'm happy to change it. |
|
Two jobs failed on 32e658d; both look unrelated to the change, so could a maintainer rerun them?
I can open a PR making this test wait for the first event, as #864 did. |
…LETED testKafkaEventReceivedByA2AServer starts subscribeToTask, which returns before the server has handled the subscribe request, and then sends the COMPLETED event to Kafka at once. When the server processes the COMPLETED event before the subscribe reads the task, the subscribe correctly rejects the task as terminal, and the test fails with "Cannot subscribe to task ... task is in terminal state". In local runs the subscribe reached the server only a few milliseconds before the COMPLETED event. Wait until the subscription delivers its first event (the initial task snapshot) before sending COMPLETED, as a2aproject#864 did for testQueueClosedEventTerminatesRemoteSubscribers. The assertions are unchanged.
|
Thanks for the quick follow-up. I reran it on 32e658d: |
|
Thanks for the rerun, @Shay-Deng. We looked into both failures again instead of asking for a rerun. build (17),
test-cloud-deployment
The new push should show the Kafka test green. If the cloud job fails again, we are happy to dig further. |
Description
This change was found and re-checked with Daavar, a conformance checker we develop at MPI-SWS (https://github.com/joulook/daavar); the issue it fixes is #1370.
CONTRIBUTINGGuide.Change.
MainEventBusProcessorgets a private monitor, held byprocessEventfrom step 1 (persist the event) to the end of step 3 (distribute it to the children), so step 2 (the replication hook and push-notification scheduling) runs under it too, and a slow push scheduler now delays subscribes. A new public method,withEventProcessingPaused(Supplier), runs an action under the monitor;onSubscribeToTaskruns its store read, not-found and terminal checks,tapandcreateOrTapfallback through it, unchanged (a privatereadAndTap);processEvent's step order is unchanged.This closes both findings: an event is now persisted either before the read (the snapshot has it, and a terminal one makes the subscribe refused, as §3.1.6 requires) or distributed after the tap (it reaches the new child queue).
Both paths take the monitor before any queue or store lock; distribution uses the non-blocking
offer. Code inside the section (aTaskStore, a replication hook or strategy, the lifecycle hook'sonSubscribe/onUnsubscribe) must not wait for the bus. The monitor is per JVM: replicated multi-instance deployments are not covered.Of the in-repo implementations (
InMemoryTaskStore,JpaDatabaseTaskStore,InMemoryQueueManager,ReplicatedQueueManagerwithReactiveMessagingReplicationStrategy,BasePushNotificationSenderwith both push-config stores,DefaultTaskStreamLifecycleHook,CloseStreamsHook), none waits for the bus or, with the default push executor, for I/O other than database round trips (with a synchronous executor, whichsetPushNotificationExecutorallows for tests, the HTTP sends run under the lock); third-party ones are not checked. With this change, a subscribe's task-store read and queue tap run while the event bus is paused, so withJpaDatabaseTaskStoreeach subscribe holds up the events of all tasks for one primary-key query (two if a stream lifecycle hook closes the task's last stream), next to the read and write the bus already does for each persisted task update. We have not measured this cost and can measure it on request. Alternatives: tapping first with a sequence filter, as a2a-rs does, needs a sequence thatTaskand the stores lack (aTaskStorecontract, JPA schema and replication change, or an in-process cache like a2a-rs's). A per-task lock needs a registry whose cleanup never hands out two locks for one live task (cancelLocksremoves its lock safely only because a cancelled task is terminal), and gains little: the bus is one loop, so once it reaches the subscribed task's event every task waits anyway. An optimistic read (sample a counter the bus bumps per event, read the store outside the lock, tap if the counter is unchanged, else retry) keeps the query out of the lock while the bus is quiet, but retries under load and needs a bound falling back to the locked read; we can offer it as a follow-up.Tests. New
SubscribeWindowTest(server-common,requesthandlers):eventPublishedBetweenReadAndTap_reachesSnapshotOrStreamandterminalStateBetweenReadAndTap_closesTheStream(#1370). AQueueManagersubclass stops the subscribe at itstap, after the store read, while the agent publishes. Each run checks that the window was exercised (the bus finished inside it, orThreadMXBeanshows the bus blocked on a lock the subscriber holds), so they cannot pass by timing. With the change, each waits out the hook's 3 s timeout by design (class: 6.8 s). Both fail onmain3f321f1cand at369ee867(#1353's merge, the issue's pin) without the change, and pass with it.Verification (JDK 17.0.21, Maven 3.10.2). On
main3f321f1c, where the diff applies without conflict: 4,135 tests, 0 failures, 0 errors, 121 skipped; at369ee867: 4,082 tests, 0 failures, 0 errors, 121 skipped. On both, all 83 reactor modules succeeded undermvn -B -fae installplusmvn -B clean install -pl boms/extras(the Extras BOM test needed the rebuild after a stale invoker-repository entry). NullAway passes.Re-check with Daavar (model at
d83d3b7,A2ATaskFixed2). In our model of a2a-java with the subscribe's read and tap made one step, as in the fix, the behaviours the specification program does not produce fell from 2,060 to 0 over the 15 scenarios of #1370, with no new kind of difference. Against our a2a-rs model, every behaviour only the fixed Java model shows (11,041) conforms to the specification.Per scenario and reproduction
Uncovered behaviours, before → after. Columns: (a) both streams kept, (b) A disconnects, (d) B subscribes at once. Rows: the executor answers with a
Message(msg) or opens the task with aTask(tasks) or a status update (taskw), split by how it completes (done).msgtasks,done-yestasks,done-notaskw,done-yestaskw,done-noRequirements as in #1370; the join takes about 3.5 minutes and 20 GiB, as there. It prints warnings that some tag names of the A2A vocabulary are written both as a unit and with one field, and a note that
Erroccurs only in the specification; neither is a failure.This PR was prepared with AI assistance and reviewed by us.
Fixes #1370