Skip to content

Commit 0216a06

Browse files
committed
chore: update _closed internal variable in connect function
1 parent 741065f commit 0216a06

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

google/cloud/sql/connector/connector.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ def connect(
244244
# connect runs sync database connections on background thread.
245245
# Async database connections should call 'connect_async' directly to
246246
# avoid hanging indefinitely.
247+
if self._closed:
248+
raise RuntimeError("Cannot connect using a closed Connector.")
247249
connect_future = asyncio.run_coroutine_threadsafe(
248250
self.connect_async(instance_connection_string, driver, **kwargs),
249251
self._loop,
@@ -468,6 +470,7 @@ def close(self) -> None:
468470
self._loop.call_soon_threadsafe(self._loop.stop)
469471
# wait for thread to finish closing (i.e. loop to stop)
470472
self._thread.join()
473+
self._closed = True
471474

472475
async def close_async(self) -> None:
473476
"""Helper function to cancel the cache's tasks

tests/unit/test_connector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ async def test_connect_async_closed_connector(
482482
connector._client = fake_client
483483
await connector.close_async()
484484
# wait for close to complete
485-
await asyncio.sleep(0.1)
485+
# await asyncio.sleep(0.1)
486486
with pytest.raises(RuntimeError) as exc_info:
487487
await connector.connect_async(
488488
"test-project:test-region:test-instance",
@@ -501,7 +501,7 @@ def test_connect_closed_connector(
501501
with Connector(credentials=fake_credentials) as connector:
502502
connector._client = fake_client
503503
connector.close()
504-
time.sleep(3.1)
504+
# time.sleep(3.1)
505505
with pytest.raises(RuntimeError) as exc_info:
506506
connector.connect(
507507
"test-project:test-region:test-instance",

0 commit comments

Comments
 (0)