-
Notifications
You must be signed in to change notification settings - Fork 209
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
sync_to_async does not find root thread inside a task #214
Comments
Is there any update on this? I think I'm experiencing something related to this bug. |
@andrewgodwin Good afternoon. Do you have a workaround for this? This is a blocker on a project for me. Here's my attempt to write a substitute for
But I think I need to set an executor. Any guidance would be much appreciated. Thank you! |
FYI I rolled back my usage of asgiref to version 3.2.10, and now the issue doesn't show up for me anymore and I don't have to write my own substitue as I had attempted above. So I suppose the issue was introduced sometime after 3.3.0. |
This issue also arises when attempting to use This, at least, is a failing test case highlighting the problem: def test_sync_to_async_inside_task():
main_thread = threading.current_thread()
sync_to_async_thread = {}
@sync_to_async
def sync_to_async_func():
sync_to_async_thread["current"] = threading.current_thread()
@async_to_sync
async def run_task():
task = asyncio.create_task(sync_to_async_func())
await task
run_task()
assert sync_to_async_thread["current"] == main_thread |
This issue impacts us mostly when running tests with
We were able to find workarounds for both issues within pytest.
All together: @pytest.mark.django_db(transaction=True)
def test_stuff(close_task_connection):
... This is slow and a bit noodly, but it's working for now for us. |
Fixes django#214. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
The event loop is ours—can we just patch its |
Fixes django#214. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Patching |
Fixes django#214. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Fixes django#214. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
@yourcelf I’m not sure if I’m looking at the right code, but it looks like Do you have a test case for |
You might be right -- I had previously grepped for |
@andersk here is a repo with as minimal a test case as I can manage with grpc's boilerplate: https://github.com/yourcelf/grpc-asgiref Test file is here: https://github.com/yourcelf/grpc-asgiref/blob/main/test_grpc_sync_to_async.py, the rest of the repo is boilerplate to set up the protobuf definition and compiled files for the grpc server to use. Sadly, #320 does not seem to address the problem -- the |
Fixes django#214. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Fixes django#214. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Thanks. I came up with a cleaner solution that also makes your test case pass: associate a Updated #320. |
Fixes django#214. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
When you use
create_task
(or any way of making a coroutine not through asgiref), and the root thread is synchronous, thensync_to_async
in thread-sensitive mode will use the root thread outside of tasks and a single, specific new thread inside of tasks, ruining the thread-sensitive guarantee.Instead, we should make
sync_to_async
always first look for an executor on the root thread and, if it finds it, use that to run code.The text was updated successfully, but these errors were encountered: