-
-
Notifications
You must be signed in to change notification settings - Fork 793
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
Django 3 + channels - runserver does not work with autoreload #1374
Comments
Same for me. Autoreload triggers the following exception:
My asgi.py:
Dependencies:
|
Thanks for the report. @andrewgodwin Pinging you on this on, since it's new: I didn't get a chance to look into it yet but, your first thought: do we need an adjustment to the |
Oh, this is very interesting indeed. It's probably Channels' job to handle, as it's the one making the outside context asynchronous, but I can't be totally sure, given there's only one channels file in the entire traceback. I suspect the Sadly, we can't use the |
Hi @andrewgodwin. I'm going to have to get you to give me an asyncio debugging lesson but... 🙂 I'm not 100% convinced that, when this error come up, we're in an
As you know, But the only loop in play is that from the Twisted reactor, which we (channels) start in our Could that be the loop that's getting returned? (How do I check that?) (It's running so, if it were...) But, we're not ourselves running inside of that event loop at the point where the exception is raised. Update: Twisted is making the exact same |
I'm going to replicate this locally and just debug it, I think, I'm really not sure what's going on here. |
It looks like raising SystemExit plunges out of twisted without it closing out the asyncio event loop, meaning that any code running "on exit" triggers async-unsafety. I've opened #1391 to detect this and manually stop the event loop in this situation. |
I'm looking at this again now. There are two threads in play. The "async" thread, running Daphne, and the "autoreloader" thread, which raises the We're in the "autoreloader" thread when we hit the issue, the "async" thread is blocked at Daphne's I'm trying to rewrite it without the rest of Django in play 🙂 |
Yeah, there's something wonky with the rest of the threads and SystemExit, which is why I wanted to write a working fix for the symptom (in that PR) while maybe eventually working out the underlying cause. It may also be Twisted's reactor at play here specifically, since I believe the Twisted reactor is across all threads, and so that might be causing it to span them? |
That makes a bit of sense. Before threading, at import we call (In contrast, I'm spinning up a thread and seeing Current status 🤔 |
OK, more or less, this is what I think is going on:
Output when run:
|
When switching threads, e.g. when run via Django auto-reloader, the default run loop causes issues detecting async contexts. Fixes django/channels#1374
Hi @swist @maxmalysh Would either or both of you be able to give the PR here a run django/daphne#294? It should do the business, but it would be good to have a confirmation. |
My little test script above, adjusted to pass a new event loop (and expect that) works well too: Output fragment:
|
Looks OK. Thanks for the fix! Dependencies:
Log:
|
Super. Thanks for taking the time to confirm @maxmalysh. I shall merge and tag now. 👍 |
When switching threads, e.g. when run via Django auto-reloader, the default run loop causes issues detecting async contexts. Fixes django/channels#1374
Updating Daphne to 2.4.1 should see you good to go. Thanks all. |
When switching threads, e.g. when run via Django auto-reloader, the default run loop causes issues detecting async contexts. Fixes django/channels#1374
I am trying to set up an example repo for Django 3 + ariadne + channels.
I have added channels and ariadne to the top of
INSTALLED_APPS
And set up
routing.py
:When I run
python manage.py runserver
I get hit with
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
I don't quite understand channels' runserver config, but I'll include trace for a good measuer:
The text was updated successfully, but these errors were encountered: