Skip to content

Using AsyncClientSession#subscribe async generator swallows the CancellationError #544

Closed

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

Activity

  1. added
    type: bug
    on May 20, 2025
  2. leszekhanusz commented on May 20, 2025

    @leszekhanusz
    Collaborator

    Thanks for your bug report. You're completely right.

    PR #548 should fix this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions