Skip to content

Commit c55d37b

Browse files
committed
Add new ssl_close_timeout argument to AIOHTTPTransport
1 parent 74d6589 commit c55d37b

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

gql/transport/aiohttp.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def __init__(
4646
auth: Optional[BasicAuth] = None,
4747
ssl: Union[SSLContext, bool, Fingerprint] = False,
4848
timeout: Optional[int] = None,
49+
ssl_close_timeout: Optional[Union[int, float]] = 10,
4950
client_session_args: Optional[Dict[str, Any]] = None,
5051
) -> None:
5152
"""Initialize the transport with the given aiohttp parameters.
@@ -55,6 +56,8 @@ def __init__(
5556
:param cookies: Dict of HTTP cookies.
5657
:param auth: BasicAuth object to enable Basic HTTP auth if needed
5758
:param ssl: ssl_context of the connection. Use ssl=False to disable encryption
59+
:param ssl_close_timeout: Timeout in seconds to wait for the ssl connection
60+
to close properly
5861
:param client_session_args: Dict of extra args passed to
5962
`aiohttp.ClientSession`_
6063
@@ -67,6 +70,7 @@ def __init__(
6770
self.auth: Optional[BasicAuth] = auth
6871
self.ssl: Union[SSLContext, bool, Fingerprint] = ssl
6972
self.timeout: Optional[int] = timeout
73+
self.ssl_close_timeout: Optional[Union[int, float]] = ssl_close_timeout
7074
self.client_session_args = client_session_args
7175
self.session: Optional[aiohttp.ClientSession] = None
7276

@@ -165,7 +169,10 @@ async def close(self) -> None:
165169
if self.session is not None:
166170
closed_event = self.create_aiohttp_closed_event(self.session)
167171
await self.session.close()
168-
await closed_event.wait()
172+
try:
173+
await asyncio.wait_for(closed_event.wait(), self.ssl_close_timeout)
174+
except asyncio.TimeoutError:
175+
pass
169176
self.session = None
170177

171178
async def execute(

tests/test_aiohttp.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,8 @@ async def handler(request):
10761076

10771077

10781078
@pytest.mark.asyncio
1079-
async def test_aiohttp_query_https(event_loop, ssl_aiohttp_server):
1079+
@pytest.mark.parametrize("ssl_close_timeout", [0, 10])
1080+
async def test_aiohttp_query_https(event_loop, ssl_aiohttp_server, ssl_close_timeout):
10801081
from aiohttp import web
10811082
from gql.transport.aiohttp import AIOHTTPTransport
10821083

@@ -1091,7 +1092,9 @@ async def handler(request):
10911092

10921093
assert str(url).startswith("https://")
10931094

1094-
sample_transport = AIOHTTPTransport(url=url, timeout=10)
1095+
sample_transport = AIOHTTPTransport(
1096+
url=url, timeout=10, ssl_close_timeout=ssl_close_timeout
1097+
)
10951098

10961099
async with Client(transport=sample_transport,) as session:
10971100

0 commit comments

Comments
 (0)