Skip to content
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

Static Files Error With The Latest Version (3.3.2) #251

Closed
GitBolt opened this issue Apr 6, 2021 · 11 comments · Fixed by #252
Closed

Static Files Error With The Latest Version (3.3.2) #251

GitBolt opened this issue Apr 6, 2021 · 11 comments · Fixed by #252

Comments

@GitBolt
Copy link

GitBolt commented Apr 6, 2021

There seems to be something wrong with serving static files in the latest version, this is the error:

raise TypeError("async_to_sync can only be applied to async functions.")
TypeError: async_to_sync can only be applied to async functions.
HTTP GET /static/rest_framework/js/default.js 500 [1.01, 127.0.0.1:50017]

The previous version (3.3.1) works perfectly

@carltongibson
Copy link
Member

carltongibson commented Apr 6, 2021

@StingBolt Can you paste the full traceback please? Also can you say if you're using this just with Django or with Channels or with..., and so on? — Context is all here. 🙂

Thanks

Related django/django#14221

@BeryJu
Copy link

BeryJu commented Apr 6, 2021

I also just ran into this issue, same error message:

Traceback (most recent call last):
  File "/Users/langhammerj/.local/share/virtualenvs/authentik-fAhVAmau/lib/python3.9/site-packages/channels/staticfiles.py", line 40, in __call__
    return await self.staticfiles_handler_class()(
  File "/Users/langhammerj/.local/share/virtualenvs/authentik-fAhVAmau/lib/python3.9/site-packages/channels/staticfiles.py", line 56, in __call__
    return await super().__call__(scope, receive, send)
  File "/Users/langhammerj/.local/share/virtualenvs/authentik-fAhVAmau/lib/python3.9/site-packages/channels/http.py", line 198, in __call__
    await self.handle(scope, async_to_sync(send), body_stream)
  File "/Users/langhammerj/.local/share/virtualenvs/authentik-fAhVAmau/lib/python3.9/site-packages/asgiref/sync.py", line 105, in __init__
    raise TypeError("async_to_sync can only be applied to async functions.")
TypeError: async_to_sync can only be applied to async functions.

(In my case, using Django 3.1.7, asgiref 3.3.2, python 3.9.2 and channels 3.0.3, and downgrading to 3.3.1 fixes it here too)

Oh, and the error also happens if I disable channels and only use django's get_asgi_application()

@KristobalJunta
Copy link
Contributor

I'm not familiar with REST framework code there, but the issue can be related to how functools.partial works on Python < 3.8.

See this SO answer for details - https://stackoverflow.com/a/52422903.

Got a related bug in another library today - datadvance/DjangoChannelsGraphqlWs#68

@pgjones
Copy link
Contributor

pgjones commented Apr 6, 2021

I've seen this bug in Quart (I think), here is a fix

@yedpodtrzitko
Copy link

yedpodtrzitko commented Apr 6, 2021

Quart code:

 if sys.version_info >= (3, 8):
        return asyncio.iscoroutinefunction(func)

...however I'm encountering this issue even with Python 3.9.1

KristobalJunta added a commit to KristobalJunta/asgiref that referenced this issue Apr 6, 2021
On Python < 3.8 a functool.partial is not detected as async function,
which is due to a bug in inspect module, see https://bugs.python.org/issue33261

This commit adds a proper type check for earlier Python versions and fixes django#251.
KristobalJunta added a commit to KristobalJunta/asgiref that referenced this issue Apr 6, 2021
On Python < 3.8 a functool.partial is not detected as async function,
which is due to a bug in inspect module, see https://bugs.python.org/issue33261

This commit adds a proper type check for earlier Python versions and fixes django#251.
KristobalJunta added a commit to KristobalJunta/asgiref that referenced this issue Apr 6, 2021
On Python < 3.8 a functool.partial is not detected as async function,
which is due to a bug in inspect module, see https://bugs.python.org/issue33261

This commit adds a proper type check for earlier Python versions and fixes django#251.
andrewgodwin pushed a commit that referenced this issue Apr 6, 2021
On Python < 3.8 a functool.partial is not detected as async function,
which is due to a bug in inspect module, see https://bugs.python.org/issue33261

This commit adds a proper type check for earlier Python versions and fixes #251.
@andrewgodwin
Copy link
Member

The issue is fixed on main, I'll get a 3.3.3 release out today as well.

KristobalJunta added a commit to KristobalJunta/asgiref that referenced this issue Apr 6, 2021
On Python < 3.8 a functool.partial is not detected as async function,
which is due to a bug in inspect module, see https://bugs.python.org/issue33261

This commit adds a proper type check for earlier Python versions and fixes django#251.
@jaknel
Copy link

jaknel commented Apr 6, 2021

This bug is not resolved for me despite upgrading to 3.3.3.

Python 3.8.6
Django 3.2
Channels 3.0.3
asgiref 3.3.3
daphne 3.0.1

It looks like a lambda created by daphne at https://github.com/django/daphne/blob/main/daphne/server.py#L206 is what eventually ends up in https://github.com/django/asgiref/blob/main/asgiref/sync.py#L120 as awaitable and fails inspect._has_code_flag(awaitable, inspect.CO_COROUTINE) which is what _iscoroutinefunction_or_partial(awaitable) eventually calls

@andrewgodwin
Copy link
Member

Yup, I think you're right. I'm going to go change the error to a warning for now, async functions are too hard to reliably detect in Python.

@andrewgodwin
Copy link
Member

3.3.4 is out which replaces this error with a warning, so your applications will still run. We'll have to figure out how to get that Daphne lambda to "look async", or maybe just abandon the whole idea of detecting coroutine-yielding callables before the fact entirely.

@carltongibson
Copy link
Member

Hey @andrewgodwin — thanks for your work on this!

This all went very quickly yesterday, and with the Django 3.2 release I didn't have the bandwidth to follow totally. I was looking at a quick release for Daphne today, but with asgiref 3.3.4 out, I will take a few days to fully work through it, and look to release next week.

We'll have to figure out how to get that Daphne lambda to "look async"...

We have django/daphne#364 proposed, to use a partial instead of the lambda.

@andrewgodwin
Copy link
Member

Yes, let's take the time and do this properly. Given we know that partial works and is accounted for in both python and asgiref, I think I'd go with that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants