Connection.disconnect got Future attached to a different loop when sending a message in a django ASGI view
See original GitHub issueJust 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:
- Created a year ago
- Comments:10 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 callinggroup_send
and the same print inside theclose
method that calls thedisconnect
function, and they are indeed different somehow.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 thefuture: ...
error after doing the monkey patch to theConnection.__del__
dunder.