11import asyncio
2- import functools
32import io
43import json
54import logging
2827from ..utils import extract_files
2928from .appsync_auth import AppSyncAuthentication
3029from .async_transport import AsyncTransport
30+ from .common .aiohttp_closed_event import create_aiohttp_closed_event
3131from .exceptions import (
3232 TransportAlreadyConnected ,
3333 TransportClosed ,
@@ -147,59 +147,6 @@ async def connect(self) -> None:
147147 else :
148148 raise TransportAlreadyConnected ("Transport is already connected" )
149149
150- @staticmethod
151- def create_aiohttp_closed_event (session ) -> asyncio .Event :
152- """Work around aiohttp issue that doesn't properly close transports on exit.
153-
154- See https://github.com/aio-libs/aiohttp/issues/1925#issuecomment-639080209
155-
156- Returns:
157- An event that will be set once all transports have been properly closed.
158- """
159-
160- ssl_transports = 0
161- all_is_lost = asyncio .Event ()
162-
163- def connection_lost (exc , orig_lost ):
164- nonlocal ssl_transports
165-
166- try :
167- orig_lost (exc )
168- finally :
169- ssl_transports -= 1
170- if ssl_transports == 0 :
171- all_is_lost .set ()
172-
173- def eof_received (orig_eof_received ):
174- try : # pragma: no cover
175- orig_eof_received ()
176- except AttributeError : # pragma: no cover
177- # It may happen that eof_received() is called after
178- # _app_protocol and _transport are set to None.
179- pass
180-
181- for conn in session .connector ._conns .values ():
182- for handler , _ in conn :
183- proto = getattr (handler .transport , "_ssl_protocol" , None )
184- if proto is None :
185- continue
186-
187- ssl_transports += 1
188- orig_lost = proto .connection_lost
189- orig_eof_received = proto .eof_received
190-
191- proto .connection_lost = functools .partial (
192- connection_lost , orig_lost = orig_lost
193- )
194- proto .eof_received = functools .partial (
195- eof_received , orig_eof_received = orig_eof_received
196- )
197-
198- if ssl_transports == 0 :
199- all_is_lost .set ()
200-
201- return all_is_lost
202-
203150 async def close (self ) -> None :
204151 """Coroutine which will close the aiohttp session.
205152
@@ -219,7 +166,7 @@ async def close(self) -> None:
219166 log .debug ("connector_owner is False -> not closing connector" )
220167
221168 else :
222- closed_event = self . create_aiohttp_closed_event (self .session )
169+ closed_event = create_aiohttp_closed_event (self .session )
223170 await self .session .close ()
224171 try :
225172 await asyncio .wait_for (closed_event .wait (), self .ssl_close_timeout )
0 commit comments