Skip to content

fix: cancel in-flight REST streaming futures on close - #1202

Merged
kabir merged 2 commits into
a2aproject:mainfrom
hutiefang76:fix/1194-rest-stream-lifecycle
Nov 25, 2026
Merged

kabir merged 2 commits into
a2aproject:mainfrom
hutiefang76:fix/1194-rest-stream-lifecycle

Conversation

@hutiefang76

@hutiefang76 hutiefang76 commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Description

Closing a REST transport completes and cancels every HTTP streaming request it still owns, in both the native and 0.3 compatibility clients. Normal terminal completion is signalled before cancellation, so deliberate close does not surface the JDK provider's cancellation IOException. If a terminal handler throws, the remaining streams are still closed and distinct exceptions are suppressed onto the first failure.

Rebased onto e55888ea096e4c9068d3c3d640f2870b4856d42c. Existing native and compatibility completion callbacks, compatibility future-publication handling, HTTP configuration and redirect behavior are preserved. Streams registered while close is in progress are also completed and cancelled. Both implementations use the same non-null future contract. Borrowed HTTP clients remain caller-owned.

Vert.x and Android futures still do not cancel their underlying network streams; this change does not add those provider hooks.

Related: #1194

Validation

  • The 24 focused close/completion tests pass on JDK 17.0.18 and JDK 21.0.10, including real JDK HTTP connection release for both protocols.
  • The 10 new review regressions fail against the rebased pre-review implementation; the 14 existing focused tests pass there. All 24 pass with this revision.
  • On macOS ARM64 / JDK 21, the affected 13-module reactor passed verify: 879 tests, zero failures, errors or skips. This full run used an external source copy with only the two pre-existing MockServer tests' fixed port changed from 4001 to 59898 because 4001 was occupied locally. Those test-port changes are not in the PR. The unmodified-port full run failed at MockServer bind setup; it is not reported as passing.
  • No tests were excluded from that full run and no failures were ignored. Hosted CI is separate from these local results.

Contribution checklist

  • Follow the CONTRIBUTING guide and existing code conventions.
  • Use a Conventional Commits title and retain the issue reference.
  • Add regression tests and run the affected module tests.
  • Reviewed documentation requirements; the provider limitations remain documented above.

AI-assisted implementation and first-pass review: DeepSeek; coordination and native verification: OpenAI Codex. Test counts above come from executed Maven reports.

@kabir kabir left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @hutiefang76 - thank you for the PR, and sorry for the delay in looking at it!

I've had a look, and think it looks good, but it needs a rebase.

Two points for the rebase:

  • Preserve the native REST sseEventListener.onComplete() callbacks added by #1236. They overlap the modified streaming calls.
  • Keep the documented limitation: Vert.x and Android futures still do not cancel their underlying network streams.

@hutiefang76

Copy link
Copy Markdown
Contributor Author

Rebased onto a2c8a9383eb3 in a78f1a63, addressing your review. The native #1236 and compatibility #1279 completion callbacks are retained, and the current HTTP configuration and redirect handling are unchanged. The Vert.x/Android limitation remains in the description.

The current affected reactor passes 798 tests with no failures, errors or skips. The 14 focused regressions pass on JVM 17 and 21, including completion preservation and real JDK connection release for both protocols.

@kabir

kabir commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Hi @hutiefang76 Sorry, we missed this. Can you rebase again?

A few observations after looking at #1355, which applies the same close pattern to the JSON-RPC transport:

1. signalTerminal(null) before cancel(true)

#1355 introduced a closeStream() method that calls signalTerminal(null) before stream.cancel(true). The reason: on JdkA2AHttpClient, cancelling a future sends an IOException("Request cancelled") through the error handler. By calling signalTerminal(null) first, the AtomicBoolean guard drops the subsequent IOException, so users don't see spurious errors from a deliberate close().

It would be great to align this PR with that approach for consistency — otherwise the REST and JSON-RPC transports would handle close differently.

2. Exception suppression in close()

If a handler throws during one stream's cancellation, the current loop would stop and leave remaining streams unclosed. #1355 handles this by collecting exceptions with addSuppressed() and rethrowing only after all streams have been closed. Worth considering the same pattern here.

3. Minor: null guard parity

RestTransport_v0_3.trackStream() — does it need a null guard on the stream parameter for safety, matching the native version (or vice versa)? Both should be consistent.

Track active REST streaming futures in both protocol implementations,
remove completed futures, and cancel pending futures when closing.
Cover completion, shutdown races, idempotence, and JDK socket release.

This fixes a2aproject#1194
Signal normal terminal completion before cancelling HTTP streams in both
REST protocol implementations. Close every stream when a terminal handler
throws, and aggregate failures without self-suppression.

Add regressions for cancellation errors, throwing handlers, and setup races.

This fixes a2aproject#1194
@hutiefang76
hutiefang76 force-pushed the fix/1194-rest-stream-lifecycle branch from a78f1a6 to 4d5a2ec Compare November 25, 2026 00:02
@hutiefang76

Copy link
Copy Markdown
Contributor Author

Rebased onto e55888ea096e4c9068d3c3d640f2870b4856d42c; the current head is 4d5a2ecb2ba3393fb28a4880b7c6e753044eb69f.

Addressed the three points in your follow-up:

  1. Both REST implementations call their existing guarded onComplete() before cancelling the HTTP future. A deliberate close therefore reports normal terminal completion before the JDK cancellation IOException can arrive.
  2. close() attempts every tracked stream even if a terminal handler throws. Cancellation is in finally; distinct runtime failures are aggregated and a shared exception instance is not self-suppressed.
  3. Both trackStream methods consistently use the HTTP client's non-null future contract. Compatibility future-publication handling and the current completion callbacks are retained.

The 10 added regressions fail on the rebased pre-review implementation, while its 14 existing focused tests pass. All 24 now pass on JDK 17 and 21, including real JDK HTTP connection release in both protocols.

The full affected 13-module verify run passed 879 tests with no failures, errors or skips. For that local full run, two pre-existing MockServer test classes used port 59898 in an external source copy because 4001 was occupied. That port adaptation is not in the PR; the unmodified-port full run failed during server bind setup. The PR description records this limit. Hosted checks for this new head are running separately.

AI-assisted implementation and first-pass review: DeepSeek; coordination and native execution: OpenAI Codex.

@kabir

kabir commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Thank you @hutiefang76

@kabir
kabir merged commit d2e2592 into a2aproject:main Nov 25, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants