Skip to content

Using AsyncClientSession#subscribe async generator swallows the CancellationError #544

Closed
@loucadufault

Description

@loucadufault

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugAn issue or pull request relating to a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions