Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#33745 closed Bug (wontfix)

There is no easy way to turn off sync_to_async and async_to_sync adapters

Reported by: abetkin Owned by: nobody
Component: Uncategorized Version: 4.0
Severity: Release blocker Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by abetkin)

Hi! I have a setup that is deployed with nginx unit (https://unit.nginx.org)

I have 1 asgi and 1 wsgi application, namely for my sync and async endpoints. All my code runs natively, there is no need to adapt my code with sync_to_async-like stuff.

If an async endpoint somehow (by mistake) gets routed to the wsgi application, it is AN ERROR for me. Because it should have been routed to the asgi app. However, django will silently adapt it and run in a separate thread.
The same happens if a sync endpoint is dispatched to the asgi application. Again, I want that to be an error!

I haven't found any easy way to turn this behavior off or even to check that this is happening, to log it somewhere. I think this is wrong.

Change History (6)

comment:1 by abetkin, 3 years ago

Description: modified (diff)

comment:2 by abetkin, 3 years ago

Description: modified (diff)

comment:3 by abetkin, 3 years ago

Description: modified (diff)

comment:4 by abetkin, 3 years ago

Description: modified (diff)

comment:5 by Carlton Gibson, 3 years ago

Resolution: wontfix
Status: newclosed

...or even to check that this is happening...

This would be a good candidate for a custom system check.

There are a number of existing URL checks you can use as an example but, the essential idea would be to check the callback of each routed URL:

>>> import asyncio
>>> from django.urls import get_resolver
>>> resolver = get_resolver()
>>> p = resolver.url_patterns[0]
>>> asyncio.iscoroutinefunction(p.callback)
False

Hopefully that gets you going. Please see TicketClosingReasons/UseSupportChannels if you need more assistance.

comment:6 by abetkin, 3 years ago

Thank you, Carlton!

Actually the thing that I missed is that you can tell from request whether it is ASGIReguest or not. And thanks for the code snippet: I was using the resolver too, but my code was less elegant

Note: See TracTickets for help on using tickets.
Back to Top