← Back to Pull requests

fix(a2a): demand-driven backpressure and explicit stall failure for streaming (issue #212) #389

Merged
konlue merged 5 files (1075 lines) on Nov 29, 2026 08:44 UTC
View on GitHub
konlue konlue commented on Nov 25, 2026 12:10 UTC
Author

Contributing to ARGI: make the A2A streaming producer demand-driven and fail an indefinitely stalled consumer explicitly (issue #212).

Describe what this PR does / why we need it

Issue #212 asks for explicit backpressure and a reliable terminal state when the A2A streaming queue is full. The 1000-slot LinkedBlockingQueue the issue quotes was already removed by #277 (cancel aborts the blocking read) and #213/#371 (structured terminal errors), but the replacement has the same unbounded-memory failure mode in a different shape: Flux.create uses the default BUFFER strategy, so a subscriber that stops requesting without cancelling makes the producer keep reading the socket and buffer without bound — the stream neither completes nor fails, and there is no capacity/wait configuration. This PR makes downstream demand the only buffer and turns an indefinitely stalled consumer into an explicit A2aBackpressureException, with a configurable wait.

Does this pull request fix one issue?

Fixes #212

Describe how you did it

The producer now emits through StreamEmission, a per-subscription demand gate. Permits are counted exactly from FluxSink.onRequest and one permit is consumed per emitted event; requestedFromDownstream() is deliberately not used as the permit source because it races with the sink's own decrement — the race let one extra event sit in the sink queue, which then held the terminal signal behind undeliverable data. The wait re-checks every 50 ms and is woken by requests, cancellation and disposal. While waiting, the producer is not reading the socket, so TCP backpressure reaches the remote peer.

The blocking exchange runs on its own bounded-elastic task instead of inside the Flux.create emitter. Running it in the emitter tied the blocking work to the subscribeOn worker, which also has to forward downstream requests; the producer then waited for demand that its own worker could never deliver (reproduced with probes before and after this change).

When the consumer stays at zero demand for streamDemandTimeout, the gate first calls post.cancel(), then delivers sink.error(A2aBackpressureException) directly and lets the producer unwind normally. Delivering the terminal before unwinding is essential: letting the exception escape the HTTP client's response handler delayed the error by roughly five seconds of connection teardown in the reproduction. Because no event is ever emitted without a permit, the sink queue is empty when the terminal is sent, and complete/error are delivered even at zero demand. The done response stays an ordinary event and therefore needs one permit — a consumer that stops requesting before it gets the explicit failure instead of an accumulated result stranded in a buffer — and a cancelled subscription still receives neither an error nor a done response, preserving the #213/#277 contracts.

There is no intermediate queue, so the effective capacity is the subscriber's outstanding demand. A2aNodeActionWithConfig.streamDemandTimeout(Duration) configures the bound (default 60 s, Duration.ZERO waits indefinitely to restore the legacy behaviour, negative values rejected) and A2aRemoteAgent.Builder.streamDemandTimeout passes it through both node construction sites, which now share one newNodeAction factory so the agent-level userId and timeout options cannot drift apart. Of the options the issue asks to compare, an unbounded queue is rejected by the acceptance criteria, and a bounded Sinks.many().unicast().onBackpressureBuffer(ArrayBlockingQueue) variant is rejected because its terminal can queue behind data that no demand can drain and its overflow wait would need polling; cancellable blocking backpressure plus an explicit error plus a terminal channel independent of demand is the chosen shape.

Describe how to verify it

Red on origin/main: A2aStreamBackpressureTest#stalledConsumerFailsWithExplicitBackpressureError subscribes with an initial request of one, stops requesting, and only times out — the old producer buffers the rest of the stream forever. The new code fails that test green with A2aBackpressureException after the configured 250 ms, having delivered exactly one chunk and released the worker.

New coverage in A2aStreamBackpressureTest (7 tests): a stalled consumer gets the explicit error in bounded time with the worker released; a slow consumer requesting one event at a time receives every chunk plus the done response in order; disposing while the producer waits for demand emits no terminal signal and releases the worker; a consumer that pauses for 500 ms and resumes within the timeout receives every chunk; a consumer that stops requesting right before the done response fails explicitly instead of hanging; Duration.ZERO keeps waiting instead of failing; and the timeout configuration rejects null/negative values while accepting Duration.ZERO.

Two further tests in A2aRemoteAgentTests cover the builder wiring: a configured timeout reaches the node action produced by the shared factory, and null/negative values are rejected while Duration.ZERO is accepted.

Existing coverage is unchanged: the A2A suite is 82 tests green, including A2aStreamCancellationTest (transport abort on cancel, #277) and A2aStructuredFailurePropagationTest (structured terminal errors, #213). The full argi-agent-framework module shows the same 2 failures and 10 errors as the known Windows baseline (filesystem symlink permissions, ripgrep executable permissions and a CreateProcess command-line limit — none A2A related), and checkstyle plus spotless are clean for the module.

Special notes for reviews

The only new failure mode is a consumer that stays at zero demand for the whole timeout; Duration.ZERO disables the timeout entirely (it does not mean "fail immediately"), restoring the previous wait-forever behaviour, and the done response follows the same demand rule as any other event. The exchange now runs on its own bounded-elastic task while the subscribeOn worker returns to the pool right after subscription, so the per-stream thread footprint is unchanged. Change size: 5 files, +961/−114 (including 9 new tests: 7 in A2aStreamBackpressureTest and 2 in A2aRemoteAgentTests). Boundary: this does not revisit the terminal classification from #213, does not touch Extensions #25 or the persistence cancellation work in #14, and does not change the graph engine.

Fixes #212

Diff: 1075 lines (+961 / -114) across 5 files Reviewer: yuluo-yx Review duration: 5554m
All Vouch review confidence checks passed
Residual risk 0.25 = Change Risk (0.49) × (1 − Review Confidence (0.48)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.49 · 1075 lines diff across 5 files
0.49
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.00 · 1 substantive comment(s)
0.00
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
48%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.400
Shallow Review Depth shallow_review_depth
0.350
Real PR Conversation Timeline
2 items
yuluo-yx yuluo-yx approved these changes on Nov 27, 2026 04:50 UTC
Approved Reviewer

LGTM

Reviewers
Assignees
konlue konlue
Labels
risk: low confidence: 48.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 48.9% (MEDIUM) · Higher = larger blast radius
48.9%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 48.5% · Lower = rushed or shallow review
48.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 25.2% · Safe to merge
25.2%