🌐 US-Proxy
class="logged-out env-production page-responsive" style="word-wrap: break-word;" >
Skip to content

bug(google-cloud-bigtable): race condition between background _register_instance and synchronous _remove_instance_registration #19158

Open

Description

@parthea

Summary

In google-cloud-bigtable's data client (google/cloud/bigtable/data/_async/client.py and generated _sync_autogen/client.py, as well as execute_query_iterator.py), _DataApiTarget.__init__ and ExecuteQueryIterator.__init__ defer in-memory instance registration to a background task via CrossSync.create_task(self.client._register_instance, ...):

  • In sync mode, CrossSync._Sync_Impl.create_task submits _register_instance to client._executor (ThreadPoolExecutor).
  • In async mode, asyncio.create_task schedules _register_instance on a future event-loop tick.

Meanwhile, close() calls client._remove_instance_registration(...) synchronously on the calling thread, and _ping_and_warm_instances() iterates self._active_instances directly.

Race Conditions

  1. Ownership leak on quick close(): If table.close() runs while _register_instance has already started on a worker thread (so self._register_instance_future.cancel() cannot cancel it) but before _register_instance inserts into _instance_owners / _active_instances, _remove_instance_registration misses the entry (KeyError -> returns False) and the background thread then inserts the already-closed target, leaking it in _active_instances for the lifetime of the client.
  2. Unsynchronized concurrent mutations (sync client): _instance_owners and _active_instances are mutated from both calling threads (_remove_instance_registration) and worker threads (_register_instance), while _manage_channel / _ping_and_warm_instances iterates self._active_instances directly without snapshotting (Set changed size during iteration risk).
  3. Sync unit test flakiness: Tests like tests/unit/data/_sync_autogen/test_client.py::TestTable::test_ctor[True] raced with the worker thread because CrossSync._Sync_Impl.yield_to_event_loop() is a no-op (pass) and CrossSync strips await on await table._register_instance_future. (Worked around in tests by waiting on _register_instance_future via CrossSync.wait(...).)

Suggested Fix

  • Make _register_instance a synchronous method (symmetric with _remove_instance_registration) that records _instance_owners and _active_instances immediately on the calling thread, returning CrossSync.Future[...] | None:
    • Only spawn a background task (CrossSync.create_task(self._ping_and_warm_instances, instance_key, sync_executor=self._executor)) when the instance is newly active and _channel_refresh_task is already running.
    • Guard _instance_owners / _active_instances mutations with a sync-only lock (threading.Lock() if not CrossSync.is_async else contextlib.nullcontext(), matching MutationsBatcherAsync._stage_lock).
    • Snapshot list(self._active_instances) in _ping_and_warm_instances.
  • Once fixed in client.py, the temporary wait/TODO workarounds in tests/unit/data/_async/test_client.py can be simplified and direct unit tests for synchronous registration + immediate close() can be added.

Activity

  1. added
    type: bug
    api: bigtable
    on Nov 20, 2026
  2. changed the title [-]fix(google-cloud-bigtable): race condition between background _register_instance and synchronous _remove_instance_registration[/-] [+]bug(google-cloud-bigtable): race condition between background _register_instance and synchronous _remove_instance_registration[/+] on Nov 20, 2026
  3. self-assigned this
    on Nov 20, 2026
  4. added
    priority: p2
    on Nov 20, 2026
  5. PCTEJA commented on Nov 26, 2026

    @PCTEJA

    @parthea, I see this is assigned to you. May I pick this up please? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: bigtablepriority: p2type: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions