Skip to content

Commit 0084b95

Browse files
authored
Close transport when fetching the schema failed (#297)
1 parent 12fc895 commit 0084b95

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

gql/client.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,15 @@ async def __aenter__(self):
271271
self.session = AsyncClientSession(client=self)
272272

273273
# Get schema from transport if needed
274-
if self.fetch_schema_from_transport and not self.schema:
275-
await self.session.fetch_schema()
274+
try:
275+
if self.fetch_schema_from_transport and not self.schema:
276+
await self.session.fetch_schema()
277+
except Exception:
278+
# we don't know what type of exception is thrown here because it
279+
# depends on the underlying transport; we just make sure that the
280+
# transport is closed and re-raise the exception
281+
await self.transport.close()
282+
raise
276283

277284
return self.session
278285

@@ -293,8 +300,15 @@ def __enter__(self):
293300
self.session = SyncClientSession(client=self)
294301

295302
# Get schema from transport if needed
296-
if self.fetch_schema_from_transport and not self.schema:
297-
self.session.fetch_schema()
303+
try:
304+
if self.fetch_schema_from_transport and not self.schema:
305+
self.session.fetch_schema()
306+
except Exception:
307+
# we don't know what type of exception is thrown here because it
308+
# depends on the underlying transport; we just make sure that the
309+
# transport is closed and re-raise the exception
310+
self.transport.close()
311+
raise
298312

299313
return self.session
300314

tests/test_client.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,50 @@ def test_gql():
200200
client = Client(schema=schema)
201201
result = client.execute(query)
202202
assert result["user"] is None
203+
204+
205+
@pytest.mark.requests
206+
def test_sync_transport_close_on_schema_retrieval_failure():
207+
"""
208+
Ensure that the transport session is closed if an error occurs when
209+
entering the context manager (e.g., because schema retrieval fails)
210+
"""
211+
212+
from gql.transport.requests import RequestsHTTPTransport
213+
214+
transport = RequestsHTTPTransport(url="http://localhost/")
215+
client = Client(transport=transport, fetch_schema_from_transport=True)
216+
217+
try:
218+
with client:
219+
pass
220+
except Exception:
221+
# we don't care what exception is thrown, we just want to check if the
222+
# transport is closed afterwards
223+
pass
224+
225+
assert client.transport.session is None
226+
227+
228+
@pytest.mark.aiohttp
229+
@pytest.mark.asyncio
230+
async def test_async_transport_close_on_schema_retrieval_failure():
231+
"""
232+
Ensure that the transport session is closed if an error occurs when
233+
entering the context manager (e.g., because schema retrieval fails)
234+
"""
235+
236+
from gql.transport.aiohttp import AIOHTTPTransport
237+
238+
transport = AIOHTTPTransport(url="http://localhost/")
239+
client = Client(transport=transport, fetch_schema_from_transport=True)
240+
241+
try:
242+
async with client:
243+
pass
244+
except Exception:
245+
# we don't care what exception is thrown, we just want to check if the
246+
# transport is closed afterwards
247+
pass
248+
249+
assert client.transport.session is None

0 commit comments

Comments
 (0)