Skip to content

Commit 27c2da8

Browse files
committed
chore: update _closed internal variable in connect function
1 parent 9a192a4 commit 27c2da8

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
@@ -243,6 +243,8 @@ def connect(
243243
# connect runs sync database connections on background thread.
244244
# Async database connections should call 'connect_async' directly to
245245
# avoid hanging indefinitely.
246+
if self._closed:
247+
raise RuntimeError("Cannot connect using a closed Connector.")
246248
connect_future = asyncio.run_coroutine_threadsafe(
247249
self.connect_async(instance_connection_string, driver, **kwargs),
248250
self._loop,
@@ -467,6 +469,7 @@ def close(self) -> None:
467469
self._loop.call_soon_threadsafe(self._loop.stop)
468470
# wait for thread to finish closing (i.e. loop to stop)
469471
self._thread.join()
472+
self._closed = True
470473

471474
async def close_async(self) -> None:
472475
"""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)