From 2b856256132e9d4c7be725b19bdcbe7d0c164298 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Sun, 29 Nov 2026 08:23:11 +0000 Subject: [PATCH 1/2] fix(spanner): harden sync system test instance scrubbing and teardown 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. --- .../tests/system/_helpers.py | 36 +++++++++++-------- .../tests/system/conftest.py | 24 ++++++------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/packages/google-cloud-spanner/tests/system/_helpers.py b/packages/google-cloud-spanner/tests/system/_helpers.py index a4695a17c3e3..9e782eeb14d2 100644 --- a/packages/google-cloud-spanner/tests/system/_helpers.py +++ b/packages/google-cloud-spanner/tests/system/_helpers.py @@ -111,7 +111,7 @@ def scrub_referencing_databases(to_scrub, db_list): for db_name in db_list: db = to_scrub.database(db_name.split("/")[-1]) try: - retry_429_503(db.delete)() + retry_429_503(db.drop)() except exceptions.NotFound: # lost the race pass @@ -127,25 +127,28 @@ def scrub_instance_backups(to_scrub): retry_429_503(bkp.delete)() except exceptions.NotFound: # lost the race pass - except exceptions.MethodNotImplemented: - # The CI emulator raises 501: local versions seem fine. + except (exceptions.MethodNotImplemented, exceptions.NotFound): + # The CI emulator raises 501, and NotFound means the instance is gone. pass def scrub_instance_ignore_not_found(to_scrub): """Helper for func:`cleanup_old_instances`""" - scrub_instance_backups(to_scrub) + 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 - for database_pb in to_scrub.list_databases(): - db = to_scrub.database(database_pb.name.split("/")[-1]) - db.reload() - try: - 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 + scrub_instance_backups(to_scrub) try: retry_429_503(to_scrub.delete)() @@ -161,7 +164,10 @@ def cleanup_old_instances(spanner_client): instance = instance_mod.Instance.from_pb(instance_pb, spanner_client) if "created" in instance.labels: - create_time = int(instance.labels["created"]) + try: + create_time = int(instance.labels["created"]) + except ValueError: + continue if create_time <= cutoff: scrub_instance_ignore_not_found(instance) diff --git a/packages/google-cloud-spanner/tests/system/conftest.py b/packages/google-cloud-spanner/tests/system/conftest.py index 757068bebaa5..30c406307dba 100644 --- a/packages/google-cloud-spanner/tests/system/conftest.py +++ b/packages/google-cloud-spanner/tests/system/conftest.py @@ -223,25 +223,23 @@ def shared_instance( edition=spanner_instance_admin.Instance.Edition.ENTERPRISE, ), ) - created_op = _helpers.retry_429_503( - spanner_client.instance_admin_api.create_instance - )(request=request) - created_op.result(instance_operation_timeout) # block until completion - instance = spanner_client.instance( shared_instance_id, instance_config.name, labels=labels ) - + created_op = _helpers.retry_429_503( + spanner_client.instance_admin_api.create_instance + )(request=request) + try: + created_op.result(instance_operation_timeout) # block until completion + _helpers.cleanup_stale_databases(instance) + yield instance + finally: + _helpers.scrub_instance_ignore_not_found(instance) else: # reuse existing instance instance = spanner_client.instance(shared_instance_id) instance.reload() - - _helpers.cleanup_stale_databases(instance) - - yield instance - - if _helpers.CREATE_INSTANCE: - _helpers.retry_429_503(instance.delete)() + _helpers.cleanup_stale_databases(instance) + yield instance @pytest.fixture(scope="session") From 268496cd4665450e555f2cfbae500ced55eb338e Mon Sep 17 00:00:00 2001 From: ohmayr Date: Sun, 29 Nov 2026 08:47:13 +0000 Subject: [PATCH 2/2] fix(spanner): warn and continue on scrub drop-protection errors --- packages/google-cloud-spanner/tests/system/_helpers.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/google-cloud-spanner/tests/system/_helpers.py b/packages/google-cloud-spanner/tests/system/_helpers.py index 9e782eeb14d2..f1d0de42f70d 100644 --- a/packages/google-cloud-spanner/tests/system/_helpers.py +++ b/packages/google-cloud-spanner/tests/system/_helpers.py @@ -16,6 +16,7 @@ import os import time import uuid +import warnings from google.api_core import datetime_helpers, exceptions from test_utils import retry @@ -145,8 +146,14 @@ def scrub_instance_ignore_not_found(to_scrub): operation.result(DATABASE_OPERATION_TIMEOUT_IN_SECONDS) except exceptions.NotFound: pass + except Exception as exc: + warnings.warn( + f"Failed to disable drop protection for database {db.name}: {exc}" + ) except exceptions.NotFound: return + except Exception as exc: + warnings.warn(f"Failed to list databases for instance {to_scrub.name}: {exc}") scrub_instance_backups(to_scrub)