Skip to content

fix(spanner): label test instances and scrub backups in teardown - #19256

Closed
ohmayr wants to merge 1 commit into
mainfrom
fix/spanner-system-test-cleanup
Closed

ohmayr wants to merge 1 commit into
mainfrom
fix/spanner-system-test-cleanup

Conversation

@ohmayr

@ohmayr ohmayr commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Context & Problem

Spanner system tests can leak instances when instance-creation tests crash without standard sweeper labels, when shared_instance teardown fails on remaining backups or drop-protected databases, or when the async suite leaves stale instances behind.

Summary of Changes

  • Add systest_instance_labels() in tests/system/_helpers.py and apply it to test_create_instance and test_create_instance_with_processing_units.
  • Harden scrub_instance_ignore_not_found() and scrub_referencing_databases() to disable drop protection before scrubbing backups, call db.drop() instead of db.delete(), and tolerate NotFound on list/reload races.
  • Wrap shared_instance creation wait and yield in try/finally calling scrub_instance_ignore_not_found() in tests/system/conftest.py.
  • Add matching async stale-instance cleanup and backup/drop-protection scrubbing in tests/system/_async/conftest.py.

Why This Approach

Reuses the existing label-based 3-hour sweeper convention across both sync and async suites while fixing real teardown races (NotFound, drop protection ordering, and db.drop) without changing production client code.

Diff size note: Changes are confined to system test helpers and fixtures across sync and async suites (121 insertions across 4 test files) to fix all identified teardown and sweeper gaps together.

Intentionally Out of Scope

  • Modifying production google-cloud-spanner client code
  • Refactors to unrelated system test suites or CI workflow configs

Verification

ruff check (including import sorting), ruff format --check, py_compile, behavioral verification of sync/async scrubbers, and two independent Owl review passes

…eardown

Label instances created in test_instance_api.py with the standard
python-spanner-systests and created labels so cleanup_old_instances
sweeps them if a run crashes. Use scrub_instance_ignore_not_found in
shared_instance teardown with try/finally around creation wait, and add
matching async stale-instance cleanup and scrubbing in _async/conftest.py.

@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 cleanup and management of stale Spanner instances and databases in system tests, introducing asynchronous cleanup helpers (_scrub_instance_ignore_not_found and _cleanup_old_instances) and ensuring standard labels are applied to newly created instances. However, a critical issue was identified in the asynchronous test configuration where the synchronous factory method to_scrub.database() is incorrectly awaited, which will raise a TypeError at runtime and cause the cleanup process to fail.

"""Delete backups, disable DB drop protection, and delete an async instance."""
try:
async for database_pb in await to_scrub.list_databases():
db = await to_scrub.database(database_pb.name.split("/")[-1])

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.

high

The to_scrub.database() method is a synchronous factory method that returns an AsyncDatabase instance without performing any network I/O. Awaiting it will raise a TypeError at runtime, which will propagate and cause the teardown/cleanup helper to fail.

Suggested change
db = await to_scrub.database(database_pb.name.split("/")[-1])
db = to_scrub.database(database_pb.name.split("/")[-1])

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.

The async Instance.database() method is an async def coroutine (it awaits db._pool.bind(db) in spanner_v1/_async/instance.py), so awaiting it here is required, same as shared_instance.database() below in this file.

try:
async for backup_pb in await to_scrub.list_backups():
for db_name in backup_pb.referencing_databases:
db = await to_scrub.database(db_name.split("/")[-1])

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.

high

The to_scrub.database() method is a synchronous factory method that returns an AsyncDatabase instance without performing any network I/O. Awaiting it will raise a TypeError at runtime, which will propagate and cause the teardown/cleanup helper to fail.

Suggested change
db = await to_scrub.database(db_name.split("/")[-1])
db = to_scrub.database(db_name.split("/")[-1])

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.

Same as above: the async Instance.database() method is an async def coroutine in spanner_v1/_async/instance.py (it awaits db._pool.bind(db)), so dropping await here would leave db as an unawaited coroutine.

@ohmayr

ohmayr commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

Splitting into 3 smaller independent PRs for easier review

@ohmayr ohmayr closed this Nov 29, 2026
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