fix(spanner): handle errors during stream restart in snapshot#1471
Conversation
There was a problem hiding this comment.
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.
698bf71 to
6659c7d
Compare
There was a problem hiding this comment.
why are you removing the trace?
There was a problem hiding this comment.
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.
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>

Handle errors during stream restart in snapshot
Root Cause
When
_restart_on_unavailablecaught aServiceUnavailableor resumableInternalServerError, it attempted to re-initialize the iterator immediately within theexceptblock. 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
Noneandcontinuethe loop, forcing the re-initialization to occur inside thetryblock. This ensures that subsequent errors during restart are properly caught and retried.Testing
Added unit tests to cover this specific behavior