Repository navigation
Description
Cancelling a task awaiting the next subscription event leaves its source running when an idle AbortSignal is supplied. Without the signal, the same cancellation closes the source immediately. This can leave a subscription’s pending I/O alive after a client disconnect or request timeout.
Reproduced on main 2bd255772890e0b32241d793b3ee10a067c20cfd (GraphQL-core 3.3.2), Python 3.14.4.
"""Public subscription cancellation with an idle AbortSignal, compared to no signal."""
import asyncio
import contextlib
import json
from graphql import build_schema, parse, subscribe
from graphql.pyutils import AbortController
async def probe(with_signal):
started = asyncio.Event()
release = asyncio.Event()
closed = asyncio.Event()
delivered = []
async def events():
try:
started.set()
await release.wait()
delivered.append('event after request cancellation')
yield {'event': 'value'}
finally:
closed.set()
source = events()
controller = AbortController()
schema = build_schema('type Query { unused: String } type Subscription { event: String }')
kwargs = {'abort_signal': controller.signal} if with_signal else {}
stream = subscribe(schema, parse('subscription { event }'), root_value={'event': source}, **kwargs)
consumer = asyncio.create_task(anext(stream))
await asyncio.wait_for(started.wait(), 1)
consumer.cancel()
with contextlib.suppress(asyncio.CancelledError):
await consumer
snapshot = {'with_signal': with_signal, 'consumer_cancelled': consumer.cancelled(),
'source_closed_after_cancel': closed.is_set(), 'signal_aborted': controller.signal.aborted,
'pending_source_tasks': [t.get_coro().__class__.__name__ for t in asyncio.all_tasks()
if t is not asyncio.current_task() and not t.done()]}
release.set()
for _ in range(10):
await asyncio.sleep(0)
snapshot['events_after_cancel'] = delivered.copy()
# Explicitly close the retained source so this reproduction leaves no work behind.
await source.aclose()
await stream.aclose()
print(json.dumps(snapshot), flush=True)
return snapshot
async def main():
control = await probe(False)
affected = await probe(True)
assert control['source_closed_after_cancel']
assert control['events_after_cancel'] == []
assert affected['source_closed_after_cancel'], 'AbortSignal causes cancelled subscription source to remain active'
asyncio.run(main())The no-signal case reports source_closed_after_cancel=true and no delivered events. With the idle signal, it reports false, retains an async_generator_asend task, and produces an event when released after cancellation. The reproduction closes the remaining source before its final assertion fails.
Executor.with_abort_signal creates the source task and waits with asyncio.wait. Caller cancellation cleans up the abort-wait task, while the source task continues. I’d like to fix this by cancelling and settling the owned source task on CancelledError, with a focused regression. Does that fit the intended behavior?
Activity
Cito commentedon Nov 17, 2026
Thanks for the report and the reproducer, @AryanRG. Yes, that is the intended behavior: cancelling the caller should have the same effect with and without an abort signal, so the owned task should be cancelled and allowed to settle before the
CancelledErrorpropagates. Go ahead with the fix.This is not limited to subscriptions, though.
with_aborted_execution_error()uses the same pattern, soasyncio.wait_for(graphql(..., abort_signal=signal), timeout)keeps running the resolvers after the timeout, while without a signal they are cancelled.IncrementalPublisher._subscribe()waits on its pendingnext_batchtask the same way, please check that one too. It would be great if the PR covered all of them, with regression tests for the subscription case and for execute. If you prefer to keep it to the subscription case, that is fine too, and I will take care of the rest.AryanRG commentedon Nov 17, 2026
Thanks @Cito, I’ll cover all three. I already have the subscription and execute cases reproduced and fixed, and I’m checking the incremental publisher before opening the PR.
AryanRG commentedon Nov 17, 2026
Covered all three in #323, @Cito. The incremental path also waits for deferred resolver cleanup, with early execution on and off. Full suites pass on Python 3.10 and 3.14, with 100% configured coverage on 3.14.