File tree Expand file tree Collapse file tree
google/cloud/sql/connector Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -160,6 +160,7 @@ def __init__(
160160 self ._cache : dict [tuple [str , bool ], MonitoredCache ] = {}
161161 self ._client : Optional [CloudSQLClient ] = None
162162 self ._closed : bool = False
163+ self ._proxy : Optional [asyncio .Task ] = None
163164
164165 # initialize credentials
165166 scopes = ["https://www.googleapis.com/auth/sqlservice.admin" ]
@@ -453,7 +454,7 @@ async def connect_async(
453454 if driver in LOCAL_PROXY_DRIVERS :
454455 local_socket_path = kwargs .pop ("local_socket_path" , "/tmp/connector-socket" )
455456 host = local_socket_path
456- start_local_proxy (
457+ self . _proxy = start_local_proxy (
457458 sock ,
458459 socket_path = f"{ local_socket_path } /.s.PGSQL.{ SERVER_PROXY_PORT } " ,
459460 loop = self ._loop
@@ -538,6 +539,13 @@ async def close_async(self) -> None:
538539 self ._closed = True
539540 if self ._client :
540541 await self ._client .close ()
542+ if self ._proxy :
543+ proxy_task = asyncio .gather (self ._proxy )
544+ try :
545+ await asyncio .wait_for (proxy_task , timeout = 0.1 )
546+ except TimeoutError :
547+ # This task runs forever so it is expected to raise this exception
548+ pass
541549 await asyncio .gather (* [cache .close () for cache in self ._cache .values ()])
542550
543551
Original file line number Diff line number Diff 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
7275async def local_communication (
You can’t perform that action at this time.
0 commit comments