Skip to content
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

RuntimeError: async generator ignored GeneratorExit #394

Closed
Jonas1312 opened this issue Feb 23, 2023 · 6 comments · Fixed by #395
Closed

RuntimeError: async generator ignored GeneratorExit #394

Jonas1312 opened this issue Feb 23, 2023 · 6 comments · Fixed by #395
Labels
type: bug An issue or pull request relating to a bug

Comments

@Jonas1312
Copy link

Hi,

When I run this code:

gql_ws_transport = WebsocketsTransport(
    url=self.ws_endpoint,
    headers=self.headers,
    subprotocols=[WebsocketsTransport.APOLLO_SUBPROTOCOL],
    init_payload={
        "headers": {"Accept": "application/json", "Content-Type": "application/json"},
        "Authorization": f"X-API-Key: {self.api_key}",
    },
)
gql_ws_client = Client(
    transport=gql_ws_transport, fetch_schema_from_transport=True
)

subscription = gql_ws_client.subscribe(MY_GRAPHQL_QUERY, variables)

for result in subscription:
    should_break = do_something()
    if should_break:
        subscription.close()

I get an error in the console:

Exception ignored in: <async_generator object Client.subscribe_async at 0x7f9858ee77a0>
RuntimeError: async generator ignored GeneratorExit

How can I break the loop without having this error printed out?

Thanks!

System info:

  • OS: macos
  • Python version: 3.7
  • gql version: 3.4.0
  • graphql-core version: 3.2.3
@leszekhanusz leszekhanusz added the type: bug An issue or pull request relating to a bug label Feb 23, 2023
@leszekhanusz
Copy link
Collaborator

The normal way to exit is simply to use break:

for result in subscription:
    should_break = do_something()
    if should_break:
        break

but the same error appears on version 3.4.0, this is a bug.

I've made the PR #395 which should solve this.

@leszekhanusz
Copy link
Collaborator

It should be fixed in the pre-release v3.5.0b3

@Jonas1312
Copy link
Author

I tested the pre-release and it seems to have fixed the bug. Thanks a lot!

@NikitaMishin
Copy link

It is possible to trigger unsubscription indirectly (not inside the loop)?
This is quite important if the events on the stream are not frequent.

@NikitaMishin
Copy link

@leszekhanusz

@leszekhanusz
Copy link
Collaborator

leszekhanusz commented Apr 26, 2023

@NikitaMishin It should be possible in sync mode by calling subscription.close() from another thread but a better idea would be to use the async mode and run a subscription inside a task. Once you don't need the subscription anymore you can cancel the task. See advanced usage for an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug An issue or pull request relating to a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants