Skip to content

fix(spanner): harden sync system test instance scrubbing and teardown - #19259

Draft
ohmayr wants to merge 2 commits into
mainfrom
fix/spanner-sync-teardown-scrub
Draft

ohmayr wants to merge 2 commits into
mainfrom
fix/spanner-sync-teardown-scrub

Conversation

@ohmayr

@ohmayr ohmayr commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

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

  • Fix scrub_referencing_databases() to call db.drop() instead of db.delete().
  • Disable database drop protection before scrubbing backups and handle NotFound on list_databases(), db.reload(), and list_backups().
  • Guard created label integer parsing in cleanup_old_instances() against ValueError.
  • Wrap shared_instance creation wait and yield in try/finally calling scrub_instance_ignore_not_found().

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

  • Modifying production Spanner client code
  • Async system test cleanup and instance-creation test labeling (tracked in separate PRs)

Verification

nox -s lint mypy-3.14 unit-3.14 unit-3.10 cover and two independent Owl review passes

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.

@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 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.

Comment on lines +137 to +149
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

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.

medium

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.

Suggested change
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
  1. 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.

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.

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)

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