Skip to content

Detecting browser closure in GraphQL subscriptions #2817

Closed Answered by imankulov
imankulov asked this question in Q&A
Discussion options

You must be logged in to vote

When the browser closes the connection, the subscription handler cancels the counter task (if I am not mistaken, here)

As outlined in Python's Task Cancellation documentation.

When a task is cancelled, asyncio.CancelledError will be raised in the task at the next opportunity. [...] In case asyncio.CancelledError is explicitly caught, it should generally be propagated when clean-up is complete.

The following modified code works for me.

@strawberry.type
class Subscription:
    @strawberry.subscription
    async def counter(self) -> AsyncGenerator[int, None]:
        try:
            for value in itertools.count():
                yield value
                print(value)
                await

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by imankulov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant