fix(spanner): handle errors during stream restart in snapshot by sinhasubham · Pull Request #1471 · googleapis/python-spanner · GitHub
Skip to content
This repository was archived by the owner on Jun 8, 2026. It is now read-only.

fix(spanner): handle errors during stream restart in snapshot#1471

Merged
sinhasubham merged 1 commit into
mainfrom
fix/rst-stream-retry
Jan 14, 2026
Merged

fix(spanner): handle errors during stream restart in snapshot#1471
sinhasubham merged 1 commit into
mainfrom
fix/rst-stream-retry

Conversation

@sinhasubham

Copy link
Copy Markdown
Contributor

Handle errors during stream restart in snapshot

Root Cause
When _restart_on_unavailable caught a ServiceUnavailable or resumable InternalServerError, it attempted to re-initialize the iterator immediately within the except block. If this re-initialization failed (e.g. due to a persistent transient error), the exception would propagate unhandled, breaking the retry loop.

Fix
This change modifies the logic to reset the iterator to None and continue the loop, forcing the re-initialization to occur inside the try block. This ensures that subsequent errors during restart are properly caught and retried.

Testing
Added unit tests to cover this specific behavior

@sinhasubham sinhasubham requested review from a team January 5, 2026 15:36
@product-auto-label product-auto-label Bot added the size: m Pull request size is medium. label Jan 5, 2026
@product-auto-label product-auto-label Bot added the api: spanner Issues related to the googleapis/python-spanner API. label Jan 5, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

@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 effectively addresses a bug where errors during a stream restart in a snapshot were not handled correctly, which could break the retry loop. The fix of resetting the iterator to None and letting the main try block handle the re-initialization is a solid approach. The added unit tests properly cover the fixed scenario.

I've included a couple of suggestions to improve code maintainability by reducing duplication in both the implementation and the new tests. These changes should make the code cleaner and easier to manage in the future.

Comment thread tests/unit/test_snapshot.py
@sinhasubham sinhasubham force-pushed the fix/rst-stream-retry branch from 698bf71 to 6659c7d Compare January 6, 2026 14:22

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.

why are you removing the trace?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We aren't removing the observability; we're consolidating it. By setting iterator = None and calling continue, the loop restarts and re-enters the trace_call block at Line 111. This ensures every retry is still traced while removing the need for duplicate tracing logic inside the except blocks. By doing this we handle the problem of error inside except block which is crashing the application without going through retry mechanism.

@sinhasubham sinhasubham merged commit c066873 into main Jan 14, 2026
30 of 31 checks passed
@sinhasubham sinhasubham deleted the fix/rst-stream-retry branch January 14, 2026 15:03
sinhasubham pushed a commit that referenced this pull request Jan 16, 2026
PR created by the Librarian CLI to initialize a release. Merging this PR
will auto trigger a release.

Librarian Version: v1.0.0
Language Image:
us-central1-docker.pkg.dev/cloud-sdk-librarian-prod/images-prod/python-librarian-generator@sha256:b8058df4c45e9a6e07f6b4d65b458d0d059241dd34c814f151c8bf6b89211209
<details><summary>google-cloud-spanner: 3.62.0</summary>

##
[3.62.0](v3.61.0...v3.62.0)
(2026-01-14)

### Features

* add uuid support (#1310)
([3b1792a](3b1792aa))

### Bug Fixes

* transaction_tag should be set on BeginTransactionRequest (#1463)
([3d3cea0](3d3cea0b))

* resolve pre-release dependency failures and sqlparse recursion (#1472)
([9ec95b7](9ec95b7d))

* handle errors during stream restart in snapshot (#1471)
([c066873](c0668735))

</details>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

api: spanner Issues related to the googleapis/python-spanner API. size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants