|
| 1 | +# Copyright 2016-2026. Couchbase, Inc. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License") |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +""" |
| 17 | +Regression test for PYCBC-1821 (the >= 4.6.0 counterpart of PYCBC-1815). |
| 18 | +AsyncClusterImpl.transactions was a bare check-then-set, so threads racing the |
| 19 | +first access could each construct and orphan their own Transactions instance. |
| 20 | +An orphaned instance still owns cleanup threads and a client-record |
| 21 | +registration, and closing it on GC can deadlock the single core IO thread |
| 22 | +against the GIL (see transactions.cxx's dealloc_transactions). |
| 23 | +
|
| 24 | +The property is synchronous and Transactions.__init__ drops the GIL for a full |
| 25 | +cluster round-trip, so the racers here are OS threads, not tasks -- the guard |
| 26 | +is a threading.Lock for the same reason. |
| 27 | +
|
| 28 | +No live cluster is needed. The property never touches the network before |
| 29 | +constructing a Transactions object, so the cluster is built with |
| 30 | +skip_connect='TEST_SKIP_CONNECT' (same mechanism couchbase/tests/connection_t.py |
| 31 | +uses) and exercised purely at the Python layer, with Transactions swapped for a |
| 32 | +fake that holds the construction window open long enough to hit the race |
| 33 | +reliably. |
| 34 | +""" |
| 35 | + |
| 36 | +import threading |
| 37 | + |
| 38 | +import pytest |
| 39 | + |
| 40 | +from acouchbase.cluster import AsyncCluster |
| 41 | +from acouchbase.logic.client_adapter import AsyncClientAdapter |
| 42 | +from couchbase.auth import PasswordAuthenticator |
| 43 | +from couchbase.options import ClusterOptions |
| 44 | + |
| 45 | +NUM_THREADS = 16 |
| 46 | +# How long the fake Transactions() takes to "construct", wide enough that |
| 47 | +# every thread's check-then-set race window stays open for the whole |
| 48 | +# stampede on the old unguarded property. |
| 49 | +CONSTRUCTION_DELAY = 0.05 |
| 50 | + |
| 51 | + |
| 52 | +class TransactionsPropertyRaceTestSuite: |
| 53 | + TEST_MANIFEST = [ |
| 54 | + 'test_close_during_first_access_closes_instance', |
| 55 | + 'test_concurrent_first_access_returns_single_instance', |
| 56 | + ] |
| 57 | + |
| 58 | + @pytest.fixture(name='tracking_transactions') |
| 59 | + def tracking_transactions_cls(self, monkeypatch): |
| 60 | + """A stand-in for acouchbase.transactions.Transactions. |
| 61 | +
|
| 62 | + Counts constructions (lock-guarded), signals once a construction is in |
| 63 | + flight, and sleeps briefly so the race window stays open regardless of |
| 64 | + thread scheduling. |
| 65 | + """ |
| 66 | + lock = threading.Lock() |
| 67 | + |
| 68 | + class _Tracker: |
| 69 | + instances_created = 0 |
| 70 | + construction_started = threading.Event() |
| 71 | + |
| 72 | + def __init__(self, cluster): |
| 73 | + self.close_count = 0 |
| 74 | + with lock: |
| 75 | + type(self).instances_created += 1 |
| 76 | + type(self).construction_started.set() |
| 77 | + threading.Event().wait(CONSTRUCTION_DELAY) |
| 78 | + |
| 79 | + def close(self): |
| 80 | + with lock: |
| 81 | + self.close_count += 1 |
| 82 | + |
| 83 | + monkeypatch.setattr('acouchbase.logic.cluster_impl.Transactions', _Tracker) |
| 84 | + return _Tracker |
| 85 | + |
| 86 | + @pytest.fixture(name='unconnected_cluster') |
| 87 | + def cluster_without_core_connection(self, couchbase_config): |
| 88 | + conn_string = couchbase_config.get_connection_string() |
| 89 | + username, pw = couchbase_config.get_username_and_pw() |
| 90 | + # No event loop is touched: skipping the create-connection request means the adapter's |
| 91 | + # lazy loop lookup never runs, and the faked Transactions never reads cluster.loop. |
| 92 | + return AsyncCluster(conn_string, |
| 93 | + ClusterOptions(PasswordAuthenticator(username, pw)), |
| 94 | + skip_connect='TEST_SKIP_CONNECT') |
| 95 | + |
| 96 | + @pytest.fixture(name='stub_adapter_close') |
| 97 | + def stub_async_adapter_close(self, monkeypatch): |
| 98 | + """Stub out the core half of close_connection(). |
| 99 | +
|
| 100 | + When not connected, AsyncClientAdapter chains a connect request ahead of the |
| 101 | + close (client_adapter.py:301), so under skip_connect the close would establish |
| 102 | + the very connection this test skipped. The sync adapter early-outs instead and |
| 103 | + needs no stub, so this keeps both tests network-free and asserting only the |
| 104 | + lock ordering. |
| 105 | + """ |
| 106 | + |
| 107 | + async def _noop(self): |
| 108 | + self._closed = True |
| 109 | + |
| 110 | + monkeypatch.setattr(AsyncClientAdapter, 'close_connection', _noop) |
| 111 | + |
| 112 | + @pytest.mark.asyncio |
| 113 | + @pytest.mark.usefixtures('stub_adapter_close') |
| 114 | + async def test_close_during_first_access_closes_instance(self, tracking_transactions, unconnected_cluster): |
| 115 | + result = {} |
| 116 | + |
| 117 | + def touch(): |
| 118 | + # Off-loop: how the property stays reachable while close() runs on the loop. |
| 119 | + result['txns'] = unconnected_cluster.transactions |
| 120 | + |
| 121 | + toucher = threading.Thread(target=touch) |
| 122 | + toucher.start() |
| 123 | + # Proceed only once the property is inside Transactions(), so close() races it. |
| 124 | + assert tracking_transactions.construction_started.wait(timeout=10), 'construction never started' |
| 125 | + await unconnected_cluster.close() |
| 126 | + toucher.join(timeout=10) |
| 127 | + assert not toucher.is_alive(), 'thread failed to join, possible deadlock' |
| 128 | + |
| 129 | + txns = result.get('txns') |
| 130 | + assert txns is not None |
| 131 | + assert tracking_transactions.instances_created == 1 |
| 132 | + assert txns.close_count == 1, 'the instance published by the racing first access was never closed' |
| 133 | + assert unconnected_cluster._impl._transactions is None, 'a live Transactions outlived cluster.close()' |
| 134 | + |
| 135 | + def test_concurrent_first_access_returns_single_instance(self, tracking_transactions, unconnected_cluster): |
| 136 | + barrier = threading.Barrier(NUM_THREADS) |
| 137 | + results = [None] * NUM_THREADS |
| 138 | + |
| 139 | + def touch(idx): |
| 140 | + barrier.wait() |
| 141 | + results[idx] = unconnected_cluster.transactions |
| 142 | + |
| 143 | + threads = [threading.Thread(target=touch, args=(i,)) for i in range(NUM_THREADS)] |
| 144 | + for t in threads: |
| 145 | + t.start() |
| 146 | + for t in threads: |
| 147 | + t.join(timeout=10) |
| 148 | + assert not t.is_alive(), 'thread failed to join, possible deadlock' |
| 149 | + |
| 150 | + assert all(r is not None for r in results) |
| 151 | + first = results[0] |
| 152 | + assert all(r is first for r in results), 'racing threads observed different Transactions instances' |
| 153 | + assert tracking_transactions.instances_created == 1 |
| 154 | + |
| 155 | + |
| 156 | +class TransactionsPropertyRaceTests(TransactionsPropertyRaceTestSuite): |
| 157 | + |
| 158 | + @pytest.fixture(scope='class', autouse=True) |
| 159 | + def manifest_validated(self): |
| 160 | + def valid_test_method(meth): |
| 161 | + attr = getattr(TransactionsPropertyRaceTests, meth) |
| 162 | + return callable(attr) and not meth.startswith('__') and meth.startswith('test') |
| 163 | + method_list = [meth for meth in dir(TransactionsPropertyRaceTests) if valid_test_method(meth)] |
| 164 | + test_list = set(TransactionsPropertyRaceTestSuite.TEST_MANIFEST).symmetric_difference(method_list) |
| 165 | + if test_list: |
| 166 | + pytest.fail(f'Test manifest not validated. Missing/extra tests: {test_list}.') |
0 commit comments