Skip to content

Close Google GenAI response streams on termination - #7250

Open
sejoung wants to merge 2 commits into
spring-projects:mainfrom
sejoung:fix/google-genai-stream-cleanup
Open

sejoung wants to merge 2 commits into
spring-projects:mainfrom
sejoung:fix/google-genai-stream-cleanup

Conversation

@sejoung

@sejoung sejoung commented Oct 29, 2026 •

Copy link
Copy Markdown

Fixes #7245

GoogleGenAiChatModel consumes the SDK's ResponseStream through Flux.fromIterable() without registering resource cleanup.
As a result, the adapter does not explicitly close the SDK response on completion, error, or cancellation.

This PR connects SDK resource cleanup to the Reactor subscription lifecycle.
It also addresses cancellation cases identified while testing the initial cleanup change.

Changes

  • Close acquired SDK streams on completion, error, and cancellation.
  • Run blocking stream creation and iteration on boundedElastic().
  • Record cancellation immediately and schedule cancellation cleanup on boundedElastic() so the cancelling thread does not wait for SDK cleanup.
  • Close SDK streams that become available after cancellation without reading further events.
  • Preserve the existing wrapping of stream-creation exceptions.
  • Report late cleanup failures through Reactor's dropped-error hook.
  • Read SDK responses on demand with Flux.generate() and use map() for one-to-one response conversion to avoid an extra SSE read

Regression tests

Tests use controlled in-memory responses while exercising the actual Google SDK SSE parsing.

Coverage includes:

  • Response cleanup on completion, error, and cancellation.
  • Stream creation and iteration on boundedElastic(), including demand from another thread.
  • Cancellation before the SDK stream becomes available.
  • Cancellation while an SDK read is blocked, verifying that the cancelling thread returns before the read is released.
  • 100 races between cancellation and stream opening, verifying that each response is closed exactly once.
  • Cleanup failures on completion and cancellation, including failures from responses arriving after cancellation.
  • Combined stream and cleanup failures.
  • Preservation of stream-creation exception wrapping.
  • Verify that the first response is delivered while the third SSE event is held back and that the next read waits for downstream demand.

Validation

  • All 254 Google GenAI module unit tests pass.
  • Spring Java Format and Checkstyle pass.
  • Scoped Maven clean packaging passes.

Limitations

This change does not introduce direct transport-level request cancellation.
An SDK network read may remain blocked until data arrives, a timeout occurs, or the transport otherwise unblocks it.
Cancellation is recorded immediately, and the cancelling thread no longer waits for SDK cleanup.

The tests do not verify cancellation against the actual Gemini service or its HTTP/2 behavior.

Release SDK streams on completion, error, and cancellation.
Run blocking stream creation, iteration, and cancellation cleanup
on boundedElastic while recording cancellation immediately.

Close responses arriving after cancellation without further reads
and report late cleanup failures through Reactor's dropped-error hook.

Add regression tests for resource cleanup, blocked reads,
cancellation races, and combined stream and cleanup failures.

Fixes spring-projects#7245

Signed-off-by: Beni Kim <sejoung@gmail.com>
Fixes spring-projects#7245

Signed-off-by: Beni Kim <sejoung@gmail.com>
@sejoung
sejoung force-pushed the fix/google-genai-stream-cleanup branch from c76b873 to fe36855 Compare November 27, 2026 05:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GoogleGenAiChatModel doesn't close the response stream on cancellation

2 participants