Skip to content

fix(server-common): read and tap a subscribed task under the event bus lock - #1371

Open
joulook wants to merge 1 commit into
a2aproject:mainfrom
joulook:issue-1370
Open

joulook wants to merge 1 commit into
a2aproject:mainfrom
joulook:issue-1370

Conversation

@joulook

@joulook joulook commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

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.

Change. MainEventBusProcessor gets a private monitor, held by processEvent from 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; onSubscribeToTask runs its store read, not-found and terminal checks, tap and createOrTap fallback through it, unchanged (a private readAndTap); 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 (a TaskStore, a replication hook or strategy, the lifecycle hook's onSubscribe/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, ReplicatedQueueManager with ReactiveMessagingReplicationStrategy, BasePushNotificationSender with 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, which setPushNotificationExecutor allows 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 with JpaDatabaseTaskStore each 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 that Task and the stores lack (a TaskStore contract, 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 (cancelLocks removes 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_reachesSnapshotOrStream and terminalStateBetweenReadAndTap_closesTheStream (#1370). A QueueManager subclass stops the subscribe at its tap, after the store read, while the agent publishes. Each run checks that the window was exercised (the bus finished inside it, or ThreadMXBean shows 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 on main 3f321f1c and at 369ee867 (#1353's merge, the issue's pin) without the change, and pass with it.

Verification (JDK 17.0.21, Maven 3.10.2). On main 3f321f1c, where the diff applies without conflict: 4,135 tests, 0 failures, 0 errors, 121 skipped; at 369ee867: 4,082 tests, 0 failures, 0 errors, 121 skipped. On both, all 83 reactor modules succeeded under mvn -B -fae install plus mvn -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 a Task (tasks) or a status update (taskw), split by how it completes (done).

executor (a) (b) (d)
msg 0 → 0 0 → 0 0 → 0
tasks,done-yes 3 → 0 4 → 0 3 → 0
tasks,done-no 323 → 0 58 → 0 323 → 0
taskw,done-yes 28 → 0 4 → 0 28 → 0
taskw,done-no 614 → 0 58 → 0 614 → 0
git clone --recurse-submodules https://github.com/joulook/daavar.git && cd daavar
git checkout d83d3b7f2e1d9c8a8a1215c6f8e6e0ff5e46d7ca && git submodule update --init --recursive
(cd traceforge && cargo build --release -p traceforge --bin conf-join)
cd benchmarks/a2a-task/rust && cargo build --release
R=target/release/tf_a2a_task; J=../../../traceforge/target/release/conf-join
(cd ../../../jmc && ./gradlew -q --no-daemon --init-script ../scripts/print-classpath.gradle :core:printTestClasspath)
CP=$(cat ../../../jmc/cp.txt); mkdir -p classes report && javac -d classes -cp "$CP" ../java/bench/a2a_task/*.java
C=n,col-a,part-tasks,done-no
$R export spec --out spec.jsonl --program a2a-task --size $C --threads 16 --model fifo -- spec,$C
java -Xss4m -Xmx2g -cp "classes:$CP" org.mpi_sws.jmc.mp.cli.MpLauncher bench.a2a_task.A2ATaskFixed2:impl,$C \
  must 0 1 "$PWD/report" "$PWD/java.jsonl" 43200 --split 16 --k 16
$J --decide spec.jsonl java.jsonl > join.out; grep -m1 '^uncovered' join.out   # 0 (323 with A2ATaskFixed)

Requirements 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 Err occurs only in the specification; neither is a failure.

This PR was prepared with AI assistance and reviewed by us.

Fixes #1370

…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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant