question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: An asyncio.Future, a coroutine or an awaitable is required

See original GitHub issue

Requirements

  • Python 3.8
  • asgiref 3.2.0
  • channels 2.4.0
  • channels-redis 2.4.2
  • Django 3.0.9,
  • gunicorn 20.1.0
  • redis 3.5.3
  • uvicorn 0.11.5
  • websockets 8.1

I run channels as gunicorn -w 5 -b 0.0.0.0:8000 --log-level debug -k uvicorn.workers.UvicornWorker config.asgi:application. Also I have nginx as proxy-server.

Logs image

class TokenAuthMiddleware:
 
    def __init__(self, inner):
        self.inner = inner
 
    def __call__(self, scope):
        return TokenAuthMiddlewareInstance(scope, self)
 
 
class TokenAuthMiddlewareInstance:
    def __init__(self, scope, middleware):
        self.middleware = middleware
        self.scope = dict(scope)
        self.inner = self.middleware.inner
 
    async def __call__(self, receive, send):
        try:
            token_key = (dict((x.split("=") for x in self.scope["query_string"].decode().split("&")))).get(
                "token", None
            )
        except ValueError:
            token_key = None
        if token_key is None:
            self.scope["user"] = AnonymousUser()
        else:
            token_data = await get_token_data(token_key)
            self.scope["user"] = await get_user(token_data["user_id"])
 
        inner = self.inner(self.scope)
        return await inner(receive, send)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
YegorDBcommented, Dec 9, 2021

But I use channels v2.4.0)

Try this out. It made like AuthMiddleware from same commit.

from django.contrib.auth.models import AnonymousUser

from channels.auth import UserLazyObject
from channels.middleware import BaseMiddleware


class AuthTokenMiddleware(BaseMiddleware):

    def populate_scope(self, scope):
        if "user" not in scope:
            scope["user"] = UserLazyObject()

    async def resolve_scope(self, scope):
        scope["user"]._wrapped = await self.get_user(scope)

    async def get_user(self, scope):
        # your initial post logic (slightly changed)
        try:
            token_key = (dict((x.split("=") for x in self.scope["query_string"].decode().split("&")))).get(
                "token", None
            )
        except ValueError:
            return AnonymousUser()
        token_data = await get_token_data(token_key)
        user = await get_user(token_data["user_id"])
        return user
0reactions
dima-dmytruk23commented, Dec 9, 2021

You added receive and send parameters to TokenAuthMiddleware.__ call__. It won’t break anything?

Since it is similar to BaseMiddleware call method it would work normally I guess.

@YegorDB But I use channels v2.4.0) https://github.com/django/channels/blob/56bb2b9afc9207d19a66d78f39a8e337cde93551/channels/middleware.py#L21

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: An asyncio.Future, a coroutine or an awaitable is ...
I'm trying to make an asynchronous web scraper using beautifulsoup and aiohttp.This is my initial code to start things.I'm getting a [TypeError: ......
Read more >
nest_asyncio error: TypeError: An asyncio.Future, a coroutine ...
I am getting the a persistent error when loading a class through spyder console. Loading the same class on my windows os does...
Read more >
An asyncio.Future, a coroutine or an awaitable is required in ...
display(df). I get the following error: TypeError: An asyncio.Future, a coroutine or an awaitable is required. I launch the script in Spyder, Anaconda....
Read more >
An asyncio.Future, a coroutine or an awaitable is required
I'm trying to make an asynchronous web scraper using beautifulsoup and aiohttp.This is my initial code to start things.I'm getting a [TypeError: An...
Read more >
Common Mistakes Using Python3 asyncio
1. Introduction · 2. RuntimeWarning: coroutine foo was never awaited · 3. Task was destroyed but it is pending! · 4. Task/Future is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found