Skip to content

Using AsyncClientSession#subscribe async generator swallows the CancellationError #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
loucadufault opened this issue Apr 2, 2025 · 0 comments

Comments

@loucadufault
Copy link

loucadufault commented Apr 2, 2025

Describe the bug

Using AsyncClientSession#subscribe async generator swallows the CancellationError, prevent proper cancellation handling of subscription tasks. The subscription still ends as expected, but does not re-raise the CancelledError which breaks the asyncio contract.

To Reproduce
Steps to reproduce the behavior:

async def subscribe_to_some_query():
    try:
        async with Client(
            transport=self.transport, fetch_schema_from_transport=True
        ) as session:
            logger.info("Subscribed")

            async for result in session.subscribe(some_query):
                print(result)
    except asyncio.CancelledError:
        logger.info("Subscription was cancelled")
        raise
    logger.info("Subscription ended")

async def main():
    task = asyncio.create_task(subscribe_to_some_query())

    asyncio.sleep(10)

    task.cancel()
    await task

Output:

Subscribed
Subscription ended

Expected behavior
Expected the iteration of the async generator to re-raise the CancelledError

System info (please complete the following information):

  • OS: macOS 15.3.1
  • Python version: Python 3.10.14
  • gql version: 3.5.0
  • graphql-core version: 3.2.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant