Repository navigation
Conversation
Previously, streaming reads and queries buffered incoming chunks without a limit while waiting for a resume token from Spanner. If a stream sent many chunks without a resume token, memory usage could grow unboundedly and row delivery to the caller would stall. With this change: - Streaming results now buffer at most 512 chunks while waiting for a resume token. - Once that limit is exceeded, buffered chunks are flushed immediately to the caller and retries are disabled so already-yielded rows cannot be duplicated. - Subsequent chunks without a resume token continue streaming directly without buffering, and any transient stream failure is propagated immediately instead of being retried. - Retries are re-enabled only if a newer resume token arrives later in the stream. - Both the synchronous and asynchronous clients are updated consistently, with unit and mock-server tests covering the threshold, failure, and recovery paths.
There was a problem hiding this comment.
Code Review
This pull request introduces a buffer limit (_MAX_BUFFERED_CHUNKS = 512) for PartialResultSet chunks in both synchronous and asynchronous snapshot implementations to prevent unbounded buffering when waiting for a resume_token. If the limit is exceeded, the chunks are flushed downstream and retries are disabled until a newer resume_token is received. Comprehensive unit and mock server tests are added to validate this behavior. The review feedback suggests adding an explanatory comment in the synchronous implementation to match the asynchronous one for consistency, and removing redundant parentheses in a generator expression to comply with PEP 8.
| add_execute_streaming_sql_error( | ||
| sql_within, _MAX_BUFFERED_CHUNKS, unavailable_status() | ||
| ) | ||
| async with self._async_database() as database: |
There was a problem hiding this comment.
Where is this function defined? I couldn't find it in this file.
There was a problem hiding this comment.
- It was removed by test(spanner): add async mock server test infrastructure and tests #19168
- chore(spanner): error on truncated or malformed result streams #19164 added a new helper method for the various tests
- When merging the fixes from those two PRs into this branch, this test was accidentally turned into dead code (it was indented 4 spaces, which made it part of the helper method added in chore(spanner): error on truncated or malformed result streams #19164 due to how Python uses indentation as function delimiters)
The bad merge is fixed in ff0622d
Previously, streaming reads and queries buffered incoming chunks without a limit while waiting for a resume token from Spanner. If a stream sent many chunks without a resume token, memory usage could grow unboundedly and row delivery to the caller would stall.
With this change: