Skip to content

Commit f5be3ac

Browse files
author
Uziel Silva
committed
fix(main): Prevent asyncio destroyed task warning
Changelog: - Return the asyncio task from `start_local_proxy` - Handle it in `close_async` to cancel it gracefully
1 parent 28c1c40 commit f5be3ac

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

google/cloud/sql/connector/connector.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ async def connect_async(
401401
if driver in LOCAL_PROXY_DRIVERS:
402402
local_socket_path = kwargs.pop("local_socket_path", "/tmp/connector-socket")
403403
host = local_socket_path
404-
start_local_proxy(
404+
self._proxy = start_local_proxy(
405405
sock,
406406
socket_path=f"{local_socket_path}/.s.PGSQL.{SERVER_PROXY_PORT}",
407407
loop=self._loop
@@ -486,6 +486,13 @@ async def close_async(self) -> None:
486486
await asyncio.gather(*[cache.close() for cache in self._cache.values()])
487487
if self._client:
488488
await self._client.close()
489+
if self._proxy:
490+
proxy_task = asyncio.gather(self._proxy)
491+
try:
492+
await asyncio.wait_for(proxy_task, timeout=0.1)
493+
except TimeoutError:
494+
pass # This task runs forever so it is expected to throw this exception
495+
489496

490497

491498
async def create_async_connector(

google/cloud/sql/connector/proxy.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def start_local_proxy(
3030
ssl_sock: ssl.SSLSocket,
3131
socket_path: Optional[str] = "/tmp/connector-socket",
3232
loop: Optional[asyncio.AbstractEventLoop] = None,
33-
):
33+
) -> asyncio.Task:
3434
"""Helper function to start a UNIX based local proxy for
3535
transport messages through the SSL Socket.
3636
@@ -40,6 +40,9 @@ def start_local_proxy(
4040
socket_path: A system path that is going to be used to store the socket.
4141
loop (asyncio.AbstractEventLoop): Event loop to run asyncio tasks.
4242
43+
Returns:
44+
asyncio.Task: The asyncio task containing the proxy server process.
45+
4346
Raises:
4447
LocalProxyStartupError: Local UNIX socket based proxy was not able to
4548
get started.
@@ -66,7 +69,7 @@ def start_local_proxy(
6669
'Local UNIX socket based proxy was not able to get started.'
6770
)
6871

69-
loop.create_task(local_communication(unix_socket, ssl_sock, socket_path, loop))
72+
return loop.create_task(local_communication(unix_socket, ssl_sock, socket_path, loop))
7073

7174

7275
async def local_communication(

0 commit comments

Comments
 (0)