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: Event loop is closed when following tutorial #1966

Closed
sean-reed opened this issue Jan 22, 2023 · 9 comments
Closed

RuntimeError: Event loop is closed when following tutorial #1966

sean-reed opened this issue Jan 22, 2023 · 9 comments

Comments

@sean-reed
Copy link

sean-reed commented Jan 22, 2023

I get the following error when following the official Channels tutorial at this step (specifically, when checking the channel layer can communicate with Redis using the Django shell):

Task exception was never retrieved
RuntimeError: Event loop is closed

I was using the current channels-redis version (4.0.0) that is installed when following the tutorial. Seems to be related to this issue django/channels_redis#332.

Downgrading to channels-redis version 3.4.1 fixes the issue. I suggest updating the tutorial to

python3 -m pip install channels-redis===3.4.1
until channels-redis fix the issue.

@carltongibson
Copy link
Member

Thanks for the report.

You can run the shell with python -m asyncio to give you a concurrent context, and then not use async_to_sync().

That's probably a nicer rewrite to have anyway, if you fancied making that.

Out of interest, what happens if you use asyncio.run() rather than async_to_sync()?

@sean-reed
Copy link
Author

Out of interest, what happens if you use asyncio.run() rather than async_to_sync()?

This gives the same error.

You can run the shell with python -m asyncio to give you a concurrent context, and then not use async_to_sync().

Nice! This works. The drawbacks are that you need to manually add the DJANGO_SETTINGS_MODULE environment variable and calling asynchronous functions with await is not introduced until the next part of the tutorial (Part 3). Maybe the small section on using the Django shell to check the channel layer can communicate with Redis can just be removed altogether from the tutorial? I don't see that it adds that much, since if it's not working then you'll find out in the next step anyway and, at the moment, that check is failing when everything is fine.

@carltongibson
Copy link
Member

carltongibson commented Jan 23, 2023

Hi @sean-reed.

I've tracked it down. The move to redis-py requires closing connections explicitly

Utilizing asyncio Redis requires an explicit disconnect of the connection ...

So to get the shell example to pass cleanly we need to do something like this:

>>> import channels.layers
>>> from asgiref.sync import async_to_sync
>>>
>>> channel_layer = channels.layers.get_channel_layer()
>>>
>>> async def closing_send(channel_layer, channel, message):
...     await channel_layer.send(channel, message)
...     await channel_layer.close_pools()
>>>
>>> async_to_sync(closing_send)(channel_layer,'test_channel', {'type': 'hello'})
>>> async_to_sync(channel_layer.receive)('test_channel')
{'type': 'hello'}

Even then we should do the same for the receive call really. (A further call would hit the same issue.)

I'll have a think about the best addition to the docs.

@carltongibson
Copy link
Member

This should be resolved by channels-redis 4.1, which will automatically close the redis connection at loop shutdown.

@lunika
Copy link

lunika commented Mar 31, 2023

Great news. Do you know when this release will be available ?
Thanks

@carltongibson
Copy link
Member

It's already out.

@lunika
Copy link

lunika commented Mar 31, 2023

I see no tag nor release on github and on pypi it's still the version 4.0.0: https://pypi.org/project/channels/

Am I missing something ?

@carltongibson
Copy link
Member

Channels-redis.

@lunika
Copy link

lunika commented Mar 31, 2023

oh my bad. Sorry 🙂

Hectruendo added a commit to ModelW/preset-django that referenced this issue Sep 7, 2023
Hectruendo added a commit to ModelW/preset-django that referenced this issue Sep 7, 2023
Hectruendo added a commit to ModelW/preset-django that referenced this issue Sep 7, 2023
Hectruendo added a commit to ModelW/preset-django that referenced this issue Sep 7, 2023
Hectruendo added a commit to ChatFAQ/ChatFAQ that referenced this issue Sep 7, 2023
…is from 4.0 to 4.1 to fix error 'RuntimeError: Event loop is closed' (django/channels#1966 (comment))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

3 participants