-
-
Notifications
You must be signed in to change notification settings - Fork 799
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
Cannot call AsyncToSync twice in one sync context for channels_redis #859
Comments
Oh, yes, right, the connection caching code in
|
Thank you for the workaround solution but now it says I am not so familiar with asyncio, sorry if I am missing something obvious :) |
Async programming is Very Hard to get right. I won't be able to help with workarounds if that didn't work, let me focus on getting this working this week so it's rock-solid and maybe write up a document on how the hell async stuff works as you need a basic understanding of it to debug stuff. |
@ahaltindis This blog post might help you. |
@andrewgodwin I didn't expect any workaround in first place but appreciated very much that you gave me anyhow. Second time just wanted you and others who may suffer same problem to know that this also didn't work. Looking forward to rock-solid version and the document. Thanks again for your great effort to community. @AlexejStukov thank you, I will check. |
2.0.2 fixed my relevant issue. Probably this is gone as well |
Edit: updated stacktrace to include more detail and added
I'm still seeing the same thing, unfortunately:
Using python 3.5.3 and updated stack containing
|
Right, 2.0.2 will have fixed the issue for some as it removes some threading, but the basic issue still remains, and I'll look into it soon. |
Well it looks like the issue might have been in Could affected people pull down and check the |
Still facing the same issue. Group messages get sent properly the first time (sometimes 2 or 3 times) and then raise a runtime error. If it helps, here's the traceback. |
Hmm, that appears to be a different issue then. Let me try and tackle that too. |
No, still same exception. I used same block code for consumer and triggered in a same way. It always gets failed in the second time. Traceback I am using this workaround for now instead of AsyncToSync. I am not sure if it is suitable for every case or elegant solution.
|
I don't know if it's relevant but sometimes I get I'm using |
Alright. I have a day to look at this today so hopefully I can get it reliably replicated and then solved. |
Could you post a full traceback of your |
Ah, if you are referring to the workaround then yes, that is an expected error, and why we have |
I am still looking into it but it seems that when I call this from the shell I don't seem to get the error ever. It only seems to occasionally happen when it happens in a request. It's just one test from one machine though so if I get other results I'll update here. |
The workaround produced by @ahaltindis appears to resolve the issue. I'm not getting that error anymore. On a sidenode: channels 2 appears to be a lot slower than version 1. I'm running on a single core VPS and I can see a delay in the frontend while work is being processed by celery. Here's the pattern I'm seeing with asyncio debug enabled:
|
Alright, I am reasonably sure I have fixed the other half of this in django/channels_redis@f5e4799 - would appreciate people confirming things are fine for them (I can no longer make it crash). |
@bastbnl Can we move discussion of speed to another issue? I'd rather not clutter this one up any more. |
Preliminary tests looking really good @andrewgodwin |
Hmmm
|
Things seem very much resolved from a few different ways I've tested. Locally in docker, then hosted with two daphne's and a proxy in front. So far things look resolved. Thanks a bunch @andrewgodwin |
Seeing this.
and also
|
@bastbnl I'm not sure what you posted - it looks like an incomplete traceback? There's no error on it. @quantumlink Can I get an example of the code that is causing this error? I need to be able to replicate it to fix it, and the easiest thing for that is to see where and how you're calling it. |
In a model I have a method with this wrapped in a try/except RuntimeError
and then in a
I seem to be getting a great deal of the websockets, but some result in the errors above. |
Yes, it's incomplete as it's produced by
I'll try to reproduce the other error as well, or find it in the logfiles (edit: got it)
|
@bastbnl That appears to be a problem inside the @quantumlink So you're saying that this code works most of the time, but then fails occasionally? Can you give me some idea of the amount of hits it's getting and the failure rate in %? |
I think one of my boxes wasn't updating to the latest master release for some reason, after forcing it seems that everything works right. Edit: Apparently I'm still getting |
Ah, alright. The error you mention is deliberate if you're somehow calling AsyncToSync from something threaded (so if you have a function that's calling group add that might be in a threaded context), as making it work correctly inside threads is more of a nightmare and one I'm not willing to tackle at this moment. I'm going to close this out as fixed, then, and ship new releases of all the packages involved so everyone can get it. |
Bug seems to be fixed for me. channels-redis 2.0.3 is not yet available in pypi though. |
Should be releasing itself now, I had forgotten to tag it. |
Not sure if it's related to this, but there's a new error when I send group messages from celery,
Messages are being sent properly, but I see multiple such exceptions in the terminal. Edit : The aioredis issue seems to have been mentioned above. If there's a related issue where I can keep up with the developments, let me know. Edit 2 : This appears to be the relevant issue django/channels_redis#71 |
I get this warning now too. Here is my code:
and i call this from ipython
and get 10 messages in ipython console:
|
Just found even simpler way to trigger this
|
Yes, that warning happens basically every time. I'm looking into it separately. It doesn't affect anything negatively, however. |
@andrewgodwin I'm still seeing the "You cannot instantiate AsyncToSync inside a thread that wasn't made using SyncToAsync" error. I'm using the latest master versions of channels and channels-redis and it works the first time but if I try it a second time I get this error. I'm using |
@bruecksen That error string is not even in the latest version of |
@andrewgodwin yes you are right, I forgot to update |
yes i get this too when triggering the broadcast to a group channel using celery periodic task but it doesn't throw this error when i fire the task from the django shell . please what is the workaround for this?? |
i was able to solve this by explicitly created it As it says, there is no current event loop in a new thread. referenced from tornadoweb/tornado#2308 (comment) full working code should look like
|
Task exception was never retrieved
future: <Task finished name='Task-378' coro=<Connection.disconnect() done, defined at /home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py:819> exception=RuntimeError("Task <Task pending name='Task-378' coro=<Connection.disconnect() running at /home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py:831>> got Future <Future pending> attached to a different loop")>
Traceback (most recent call last):
File "/home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py", line 831, 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-378' coro=<Connection.disconnect() running at /home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py:831>> got Future <Future pending> attached to a different loop
Task exception was never retrieved
future: <Task finished name='Task-381' coro=<Connection.disconnect() done, defined at /home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py:819> exception=RuntimeError("Task <Task pending name='Task-381' coro=<Connection.disconnect() running at /home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py:831>> got Future <Future pending> attached to a different loop")>
Traceback (most recent call last):
File "/home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py", line 831, 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-381' coro=<Connection.disconnect() running at /home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py:831>> got Future <Future pending> attached to a different loop
Task exception was never retrieved
future: <Task finished name='Task-382' coro=<Connection.disconnect() done, defined at /home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py:819> exception=RuntimeError("Task <Task pending name='Task-382' coro=<Connection.disconnect() running at /home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py:831>> got Future <Future pending> attached to a different loop")>
Traceback (most recent call last):
File "/home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py", line 831, 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-382' coro=<Connection.disconnect() running at /home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py:831>> got Future <Future pending> attached to a different loop
sometimes I got Task exception was never retrieved
future: <Task finished name='Task-348' coro=<Connection.disconnect() done, defined at /home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py:819> exception=RuntimeError('Event loop is closed')>
Traceback (most recent call last):
File "/home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py", line 828, in disconnect
self._writer.close() # type: ignore[union-attr]
File "/usr/lib/python3.10/asyncio/streams.py", line 338, in close
return self._transport.close()
File "/usr/lib/python3.10/asyncio/selector_events.py", line 698, in close
self._loop.call_soon(self._call_connection_lost, None)
File "/usr/lib/python3.10/asyncio/base_events.py", line 750, in call_soon
self._check_closed()
File "/usr/lib/python3.10/asyncio/base_events.py", line 515, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Task exception was never retrieved
future: <Task finished name='Task-349' coro=<Connection.disconnect() done, defined at /home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py:819> exception=RuntimeError('Event loop is closed')>
Traceback (most recent call last):
File "/home/elabbasy/Desktop/BOT_TOOL/webserver/server/venv/lib/python3.10/site-packages/redis/asyncio/connection.py", line 828, in disconnect
self._writer.close() # type: ignore[union-attr]
File "/usr/lib/python3.10/asyncio/streams.py", line 338, in close
return self._transport.close()
File "/usr/lib/python3.10/asyncio/selector_events.py", line 698, in close
self._loop.call_soon(self._call_connection_lost, None)
File "/usr/lib/python3.10/asyncio/base_events.py", line 750, in call_soon
self._check_closed()
File "/usr/lib/python3.10/asyncio/base_events.py", line 515, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed |
I am using a SyncConsumer which adds itself to a group on
websocket.connect
withgroup_add
method of channel_layer.If I try to send message to this group using
group_send
method that is wrapped with AsyncToSync, first message is succeed but further messages throw this exception:>>> c = get_channel_layer()
>>> AsyncToSync(c.group_send)('task-i-1', {'type': 'task.message', 'text':{}})
>>> AsyncToSync(c.group_send)('task-i-1', {'type': 'task.message', 'text':{}})
Connection <RedisConnection [db:0]> has pending commands, closing it.
Traceback (most recent call last):
File "/home/ahmet/websocket_channel/env/lib64/python3.6/site-packages/channel
s_redis/core.py", line 316, in group_send
await connection.zremrangebyscore(key, min=0, max=int(time.time()) - self.group_expiry)
RuntimeError: Task <Task pending coro=<AsyncToSync.main_wrap() running at /home/ahmet/webso
cket_channel/env/lib64/python3.6/site-packages/asgiref/sync.py:57> cb=[_run_until_complete_cb()
at /usr/lib64/python3.6/asyncio/base_events.py:176]> got Future attached to a different loop
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "", line 1, in
File "/home/ahmet/websocket_channel/env/lib64/python3.6/site-packages/asgiref
/sync.py", line 49, in call
call_result.result()
File "/usr/lib64/python3.6/concurrent/futures/_base.py", line 425, in result
return self.__get_result()
File "/usr/lib64/python3.6/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/home/ahmet/websocket_channel/env/lib64/python3.6/site-packages/asgiref
/sync.py", line 57, in main_wrap
result = await self.awaitable(*args, **kwargs)
File "/home/ahmet/websocket_channel/env/lib64/python3.6/site-packages/channel
s_redis/core.py", line 320, in group_send
await connection.zrange(key, 0, -1)
File "/home/ahmet/websocket_channel/env/lib64/python3.6/site-packages/aioredi
s/commands/init.py", line 152, in exit
self._release_callback(conn)
File "/home/ahmet/websocket_channel/env/lib64/python3.6/site-packages/aioredi
s/pool.py", line 361, in release
conn.close()
File "/home/ahmet/websocket_channel/env/lib64/python3.6/site-packages/aioredi
s/connection.py", line 352, in close
self._do_close(ConnectionForcedCloseError())
File "/home/ahmet/websocket_channel/env/lib64/python3.6/site-packages/aioredi
s/connection.py", line 359, in _do_close
self._writer.transport.close()
File "/usr/lib64/python3.6/asyncio/selector_events.py", line 621, in close
self._loop.call_soon(self._call_connection_lost, None)
File "/usr/lib64/python3.6/asyncio/base_events.py", line 574, in call_soon
self._check_closed()
File "/usr/lib64/python3.6/asyncio/base_events.py", line 357, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
The text was updated successfully, but these errors were encountered: