Skip to content

Commit 9a84f0b

Browse files
authored
Fix subscription task cancel exception swallow (#548)
1 parent a87d97a commit 9a84f0b

File tree

5 files changed

+57
-22
lines changed

5 files changed

+57
-22
lines changed

gql/transport/common/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,7 @@ async def subscribe(
317317
if listener.send_stop:
318318
await self._stop_listener(query_id)
319319
listener.send_stop = False
320-
if isinstance(e, GeneratorExit):
321-
raise e
320+
raise e
322321

323322
finally:
324323
log.debug(f"In subscribe finally for query_id {query_id}")

tests/test_aiohttp_websocket_graphqlws_subscription.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,24 @@ async def test_aiohttp_websocket_graphqlws_subscription_task_cancel(
292292
count = 10
293293
subscription = gql(subscription_str.format(count=count))
294294

295+
task_cancelled = False
296+
295297
async def task_coro():
296298
nonlocal count
297-
async for result in session.subscribe(subscription):
299+
nonlocal task_cancelled
298300

299-
number = result["number"]
300-
print(f"Number received: {number}")
301+
try:
302+
async for result in session.subscribe(subscription):
301303

302-
assert number == count
304+
number = result["number"]
305+
print(f"Number received: {number}")
303306

304-
count -= 1
307+
assert number == count
308+
309+
count -= 1
310+
except asyncio.CancelledError:
311+
print("Inside task cancelled")
312+
task_cancelled = True
305313

306314
task = asyncio.ensure_future(task_coro())
307315

@@ -317,6 +325,7 @@ async def cancel_task_coro():
317325
await asyncio.gather(task, cancel_task)
318326

319327
assert count > 0
328+
assert task_cancelled is True
320329

321330

322331
@pytest.mark.asyncio

tests/test_aiohttp_websocket_subscription.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,16 +283,24 @@ async def test_aiohttp_websocket_subscription_task_cancel(
283283
count = 10
284284
subscription = gql(subscription_str.format(count=count))
285285

286+
task_cancelled = False
287+
286288
async def task_coro():
287289
nonlocal count
288-
async for result in session.subscribe(subscription):
290+
nonlocal task_cancelled
289291

290-
number = result["number"]
291-
print(f"Number received: {number}")
292+
try:
293+
async for result in session.subscribe(subscription):
292294

293-
assert number == count
295+
number = result["number"]
296+
print(f"Number received: {number}")
294297

295-
count -= 1
298+
assert number == count
299+
300+
count -= 1
301+
except asyncio.CancelledError:
302+
print("Inside task cancelled")
303+
task_cancelled = True
296304

297305
task = asyncio.ensure_future(task_coro())
298306

@@ -308,6 +316,7 @@ async def cancel_task_coro():
308316
await asyncio.gather(task, cancel_task)
309317

310318
assert count > 0
319+
assert task_cancelled is True
311320

312321

313322
@pytest.mark.asyncio

tests/test_graphqlws_subscription.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,16 +290,24 @@ async def test_graphqlws_subscription_task_cancel(
290290
count = 10
291291
subscription = gql(subscription_str.format(count=count))
292292

293+
task_cancelled = False
294+
293295
async def task_coro():
294296
nonlocal count
295-
async for result in session.subscribe(subscription):
297+
nonlocal task_cancelled
296298

297-
number = result["number"]
298-
print(f"Number received: {number}")
299+
try:
300+
async for result in session.subscribe(subscription):
299301

300-
assert number == count
302+
number = result["number"]
303+
print(f"Number received: {number}")
301304

302-
count -= 1
305+
assert number == count
306+
307+
count -= 1
308+
except asyncio.CancelledError:
309+
print("Inside task cancelled")
310+
task_cancelled = True
303311

304312
task = asyncio.ensure_future(task_coro())
305313

@@ -315,6 +323,7 @@ async def cancel_task_coro():
315323
await asyncio.gather(task, cancel_task)
316324

317325
assert count > 0
326+
assert task_cancelled is True
318327

319328

320329
@pytest.mark.asyncio

tests/test_websocket_subscription.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,24 @@ async def test_websocket_subscription_task_cancel(client_and_server, subscriptio
210210
count = 10
211211
subscription = gql(subscription_str.format(count=count))
212212

213+
task_cancelled = False
214+
213215
async def task_coro():
214216
nonlocal count
215-
async for result in session.subscribe(subscription):
217+
nonlocal task_cancelled
216218

217-
number = result["number"]
218-
print(f"Number received: {number}")
219+
try:
220+
async for result in session.subscribe(subscription):
219221

220-
assert number == count
222+
number = result["number"]
223+
print(f"Number received: {number}")
221224

222-
count -= 1
225+
assert number == count
226+
227+
count -= 1
228+
except asyncio.CancelledError:
229+
print("Inside task cancelled")
230+
task_cancelled = True
223231

224232
task = asyncio.ensure_future(task_coro())
225233

@@ -235,6 +243,7 @@ async def cancel_task_coro():
235243
await asyncio.gather(task, cancel_task)
236244

237245
assert count > 0
246+
assert task_cancelled is True
238247

239248

240249
@pytest.mark.asyncio

0 commit comments

Comments
 (0)