Repository navigation
Conversation
Spanner sync system test teardown can leak instances or crash when shared_instance creation times out, when backups or drop-protected databases remain, when scrub_referencing_databases calls nonexistent db.delete(), or when parallel CI runs race on NotFound.
There was a problem hiding this comment.
Code Review
This pull request improves the robustness of the system test cleanup and setup processes. It updates database deletion to use drop(), handles potential NotFound exceptions when scrubbing instance backups, wraps the database drop protection disabling in a try-except block, and handles ValueError when parsing instance creation times. Additionally, it refactors the shared_instance fixture in conftest.py to ensure instances are properly scrubbed in a finally block. The review feedback suggests making the database cleanup in scrub_instance_ignore_not_found even more robust by catching all exceptions during reload and update operations, preventing a single failure from aborting the entire cleanup process and causing resource leaks.
| try: | ||
| for database_pb in to_scrub.list_databases(): | ||
| db = to_scrub.database(database_pb.name.split("/")[-1]) | ||
| try: | ||
| db.reload() | ||
| if db.enable_drop_protection: | ||
| db.enable_drop_protection = False | ||
| operation = db.update(["enable_drop_protection"]) | ||
| operation.result(DATABASE_OPERATION_TIMEOUT_IN_SECONDS) | ||
| except exceptions.NotFound: | ||
| pass | ||
| except exceptions.NotFound: | ||
| return |
There was a problem hiding this comment.
In a teardown/cleanup helper, we want to be as robust as possible. Currently, if db.reload(), db.update(), or operation.result() raises any exception other than exceptions.NotFound (such as concurrent.futures.TimeoutError or transient gRPC errors like ServiceUnavailable), the exception will propagate and abort the entire cleanup process. This would prevent other databases, backups, and the instance itself from being deleted, leading to resource leaks.
When cleaning up multiple resources, wrap each cleanup call in a try-except block to handle exceptions individually, log any failures as warnings, and ensure that a failure in cleaning up one resource does not prevent other resources from being cleaned up.
| try: | |
| for database_pb in to_scrub.list_databases(): | |
| db = to_scrub.database(database_pb.name.split("/")[-1]) | |
| try: | |
| db.reload() | |
| if db.enable_drop_protection: | |
| db.enable_drop_protection = False | |
| operation = db.update(["enable_drop_protection"]) | |
| operation.result(DATABASE_OPERATION_TIMEOUT_IN_SECONDS) | |
| except exceptions.NotFound: | |
| pass | |
| except exceptions.NotFound: | |
| return | |
| try: | |
| for database_pb in to_scrub.list_databases(): | |
| db = to_scrub.database(database_pb.name.split('/')[-1]) | |
| try: | |
| db.reload() | |
| if db.enable_drop_protection: | |
| db.enable_drop_protection = False | |
| operation = db.update(['enable_drop_protection']) | |
| operation.result(DATABASE_OPERATION_TIMEOUT_IN_SECONDS) | |
| except Exception as e: | |
| import warnings | |
| warnings.warn(f"Failed to disable drop protection for database {db.name}: {e}") | |
| except exceptions.NotFound: | |
| return | |
| except Exception as e: | |
| import warnings | |
| warnings.warn(f"Failed to list databases: {e}") |
References
- When closing multiple resources during cleanup, wrap each close call in a try-except block to handle exceptions individually, log any failures as warnings, and use a finally block to clear the resource references.
There was a problem hiding this comment.
Updated scrub_instance_ignore_not_found() to catch and warn on non-NotFound errors when listing databases or disabling drop protection so cleanup continues to the remaining databases, backups, and instance deletion. Kept the explicit NotFound handling so concurrent deletions still stay quiet, and moved the warnings import to module level. (commit 268496cd)
Context & Problem
Spanner sync system test teardown can leak instances or crash when shared_instance creation times out, when backups or drop-protected databases remain, when scrub_referencing_databases calls nonexistent db.delete(), or when parallel CI runs race on NotFound.
Summary of Changes
Why This Approach
Fixes concrete teardown bugs in the existing sync helpers and fixture without touching production client code or async test files.
Intentionally Out of Scope
Verification
nox -s lint mypy-3.14 unit-3.14 unit-3.10 cover and two independent Owl review passes