Skip to content

fix(spanner): add stale-instance cleanup and scrubbing to async tests - #19260

Closed
ohmayr wants to merge 1 commit into
mainfrom
fix/spanner-async-stale-cleanup
Closed

ohmayr wants to merge 1 commit into
mainfrom
fix/spanner-async-stale-cleanup

Conversation

@ohmayr

@ohmayr ohmayr commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Context & Problem

The Spanner async system test suite creates labeled instances when GOOGLE_CLOUD_TESTS_CREATE_SPANNER_INSTANCE is set, but never sweeps stale instances on startup and calls raw instance.delete() without scrubbing backups or drop-protected databases or guarding creation waits.

Summary of Changes

  • Add async _scrub_instance_ignore_not_found() and _cleanup_old_instances() in tests/system/_async/conftest.py mirroring the sync suite.
  • Call _cleanup_old_instances() in async shared_instance setup and wrap creation wait and yield in try/finally calling _scrub_instance_ignore_not_found().

Why This Approach

Brings async system test cleanup to parity with sync tests using native async APIs and keeping sync/async boundaries clean.

Intentionally Out of Scope

  • Modifying production Spanner client code
  • Sync system test helpers and instance-creation 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

The Spanner async system test suite creates labeled instances when GOOGLE_CLOUD_TESTS_CREATE_SPANNER_INSTANCE is set, but never sweeps stale instances on startup and calls raw instance.delete() without scrubbing backups or drop-protected databases or guarding creation waits.

@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 introduces automated cleanup of stale system-test instances and backups in the asynchronous test configuration. It adds helper functions to sweep old instances and safely scrub databases, drop protection, and backups, integrating these into the shared_instance fixture lifecycle. However, there are critical runtime issues where the synchronous factory method Instance.database is incorrectly awaited on lines 31 and 48, which will raise a TypeError and should be corrected.

"""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 Instance.database method is a synchronous factory method that returns a Database object. Awaiting it will raise a TypeError at runtime because the returned object is not awaitable. Remove the await keyword.

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.

to_scrub is an async Instance (google.cloud.spanner_v1._async.instance.Instance), where database() is an async def that awaits db._pool.bind(db). Removing await here would return an unawaited coroutine and break db.reload().

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 Instance.database method is a synchronous factory method. Awaiting it will raise a TypeError at runtime. Remove the await keyword.

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: to_scrub is an async Instance (google.cloud.spanner_v1._async.instance.Instance), where database() is async def because it awaits db._pool.bind(db). Keeping await here to match the rest of tests/system/_async/.

@ohmayr

ohmayr commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

Closing speculative hardening PR without live leaked-resource evidence

@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