Skip to content

fix(spanner): close underlying stream on streamed result set teardown - #19156

Open
olavloite wants to merge 1 commit into
mainfrom
spanner-close-underlying-stream
Open

olavloite wants to merge 1 commit into
mainfrom
spanner-close-underlying-stream

Conversation

@olavloite

Copy link
Copy Markdown
Contributor

Previously, ending iteration early, failing mid-stream, or encountering an error during single-row lookups left the underlying response stream open until garbage collection. As a result, streaming resources could linger and operation completion metrics would not be recorded right away.

This change adds an internal close helper to streamed result sets and ensures iteration, single-row lookups, and dictionary conversion always clean up the underlying stream when finished, closed early, or interrupted by an error. Result set metadata and statistics remain accessible after closing, and no new public API is introduced.

Previously, ending iteration early, failing mid-stream, or encountering an
error during single-row lookups left the underlying response stream open
until garbage collection. As a result, streaming resources could linger
and operation completion metrics would not be recorded right away.

This change adds an internal close helper to streamed result sets and
ensures iteration, single-row lookups, and dictionary conversion always
clean up the underlying stream when finished, closed early, or interrupted
by an error. Result set metadata and statistics remain accessible after
closing, and no new public API is introduced.
@olavloite
olavloite requested a review from a team as a code owner November 20, 2026 17:31

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request implements idempotent closing of the underlying response iterator in both synchronous and asynchronous StreamedResultSet classes. It introduces a private _close method and ensures it is called within finally blocks during stream consumption (__iter__/__aiter__), one_or_none(), and to_dict_list(). Corresponding unit and mock server tests have been added to verify this behavior and ensure metrics are correctly recorded upon closing. The review feedback recommends explicitly catching asyncio.CancelledError alongside Exception in the async _close method to prevent unhandled cancellation exceptions, since CancelledError inherits from BaseException.

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

Development

Successfully merging this pull request may close these issues.

1 participant