Skip to content

Commit 5cda924

Browse files
fix: stop event loop on Connector.close (#410)
1 parent 9a4498f commit 5cda924

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

google/cloud/sql/connector/connector.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,12 @@ def close(self) -> None:
284284
)
285285
# Will attempt to safely shut down tasks for 5s
286286
close_future.result(timeout=5)
287+
# if background thread exists for Connector, clean it up
288+
if self._thread:
289+
# stop event loop running in background thread
290+
self._loop.call_soon_threadsafe(self._loop.stop)
291+
# wait for thread to finish closing (i.e. loop to stop)
292+
self._thread.join()
287293

288294
async def close_async(self) -> None:
289295
"""Helper function to cancel Instances' tasks

tests/unit/test_connector.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ def test_connect_with_unsupported_driver(connector: Connector) -> None:
8787
)
8888
# assert custom error message for unsupported driver is present
8989
assert exc_info.value.args[0] == "Driver 'bad_driver' is not supported."
90-
connector.close()
9190

9291

9392
@pytest.mark.asyncio
@@ -157,3 +156,14 @@ async def test_create_async_connector() -> None:
157156
connector = await create_async_connector()
158157
assert connector._loop == asyncio.get_running_loop()
159158
await connector.close_async()
159+
160+
161+
def test_Connector_close_kills_thread() -> None:
162+
"""Test that Connector.close kills background threads."""
163+
# open and close Connector object
164+
connector = Connector()
165+
# verify background thread exists
166+
assert connector._thread
167+
connector.close()
168+
# check that connector thread is no longer running
169+
assert connector._thread.is_alive() is False

0 commit comments

Comments
 (0)