question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connection.disconnect got Future attached to a different loop when sending a message in a django ASGI view

See original GitHub issue

Just upgraded my project to 4.0.0b1 (with also updating channels/daphne/etc) and started to get this error after doing a layer.group_send inside a django ASGI view (i.e. not a channels consumer):

Task exception was never retrieved
future: <Task finished name='Task-153' coro=<Connection.disconnect() done, defined at /home/bellini/dev/2u/backend/.venv/lib/python3.10/site-packages/redis/asyncio/connection.py:822> exception=RuntimeError("Task <Task pending name='Task-153' coro=<Connection.disconnect() running at /home/bellini/dev/2u/backend/.venv/lib/python3.10/site-packages/redis/asyncio/connection.py:837>> got Future <Future pending> attached to a different loop")>
Traceback (most recent call last):
  File "/home/bellini/dev/2u/backend/.venv/lib/python3.10/site-packages/redis/asyncio/connection.py", line 837, in disconnect
    await self._writer.wait_closed()  # type: ignore[union-attr]
  File "/usr/lib/python3.10/asyncio/streams.py", line 344, in wait_closed
    await self._protocol._get_close_waiter(self)
RuntimeError: Task <Task pending name='Task-153' coro=<Connection.disconnect() running at /home/bellini/dev/2u/backend/.venv/lib/python3.10/site-packages/redis/asyncio/connection.py:837>> got Future <Future pending> attached to a different loop

It seems that this is scheduled from the Connection.__del__ method.

obs. This does not break the view itself, but I see that message everytime. e.g. I have a post_save signal that will do a group_send to inform some listeners that the object has changes. When saving the object inside django admin or even in another view the group_send is executed correctly, the message is received by the listeners correctly and the view returns the response correctly, but I always see that traceback in my terminal

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
bellini666commented, Sep 22, 2022

Hi @carltongibson ,

I’m running this under ASGI, it works both with runserver using daphne and also when running gunicorn with uvicorn worker.

I mentioned this in this PR which fixes a similar issue for PubSub layer. In there the issue is really giving it a chance to close, but in this case it is strange that the loops are indeed not the same.

I did print(id(asyncio.get_running_loop())) before calling group_send and the same print inside the close method that calls the disconnect function, and they are indeed different somehow.

0reactions
aerickson1337commented, Nov 10, 2022

Doing some more testing that monkey patch seems to have stopped those future errors for me consistently (which is honestly weird). I can’t reliably reproduce #332 but I’ll seldomly get that error when testing, just without the future: ... error after doing the monkey patch to the Connection.__del__ dunder.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot call AsyncToSync twice in one sync context ... - GitHub
I am using a SyncConsumer which adds itself to a group on websocket.connect with group_add method of channel_layer. class TaskConsumer( ...
Read more >
asyncio 'The future belongs to a different loop than the one ...
I managed to get the two processes running but now need to call an async function directly but can't seem to do so....
Read more >
ASGI: Dead persistent postgres connections are not closed ...
The problem is the connection handler create a new databasewrapper for the async view due to thread_critical. The comment already points out the...
Read more >
3 Effective Examples of Django Async Views without Sleeping
Note that this particular view is not invoking anything asynchronously. If Django is running in the classic WSGI mode, then a new event...
Read more >
ASGI Event Loop Gotcha - Rob Blackbourn
Event(), via a web page served by the Uvicorn ASGI server, using the bareASGI framework. ... got Future <Future pending> attached to a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found