Git Product home page Git Product logo

Comments (9)

itsjef avatar itsjef commented on September 15, 2024 1

I have created an example here: https://github.com/itsjef/django-supertokens-example

Anyway, you are right @KShivendu. The problem is not with supertokens-python, but gunicorn. I have been using gevent worker class for Gunicorn, which doesn't play well with asyncio:

$ gunicorn -k gevent -b '0.0.0.0:8000' app.wsgi:application

The problem wouldn't happen when I used Django's built-in development server (python manage.py runserver).

In case of Gunicorn, I'm gonna have to revert to the default sync worker, I am unsure if that would affect performance but that's another story.

from supertokens-python.

itsjef avatar itsjef commented on September 15, 2024

I tried defining create_new_session as a synchronous function and wrapped with with sync_to_async before reassigning to original_implementation.create_new_session but it raised

RuntimeError: This event loop is already running

from supertokens-python.

KShivendu avatar KShivendu commented on September 15, 2024

I see. I tried something similar and it worked for me.

def override_session_functions(oi: SessionRecipeImplementation):
    oi_create_new_session = oi.create_new_session

    async def get_all_permissions(user_id):
        from .models import Authors
        try:
            author = await Authors.objects.aget(user_id=user_id)
            return ["author"]
        except Exception as e:
            return []

    async def create_new_session(request, user_id, access_token_payload, session_data, user_context):
        if access_token_payload is None:
            access_token_payload = {}

        permissions = await get_all_permissions(user_id)
        access_token_payload["scope"] = " ".join(permissions)
        print(permissions)

        new_session = await oi_create_new_session(request, user_id, access_token_payload, session_data, user_context)
        return new_session

    oi.create_new_session = create_new_session
    return oi

Can you recheck your django version and tell me how are you running it?
I got django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async only when I tried to call UserLastSeen.objects.get(user_id=user_id) (not aget) inside async get_all_permissions()

Also, in case you want to use, sync_to_async. You can try:

from asgiref.sync import sync_to_async

def get_all_permissions(user_id: str):
        UserModel = get_user_model()

        try:
            user = UserModel.objects.get(pk=user_id) # .aget() won't work anymore 
            return user.get_all_permissions()
        except UserModel.DoesNotExist:
            return [] # Note that returning [] makes it cleaner :)

async def create_new_session():
   ...
   permissions = await sync_to_async(get_all_permissions)(user_id)
   access_token_payload['scope'] = " ".join(permissions)
   ...

Let me know if it helps :)

from supertokens-python.

KShivendu avatar KShivendu commented on September 15, 2024

Ohh wait. You're using:

user = await UserModel.objects.defer("groups", "user_permissions").aget(pk=user_id)

I doubt if .defer() isn't aligned with django's new async ORM features. Since it is part of the chain, I'd expect django to possibly throw django.core.exceptions.SynchronousOnlyOperation

I haven't tested this but if my assumption is correct, this should work:

async def get_all_permission():
    ....
   query_adefer = sync_to_async(UserModel.objects.defer)
   user = await query_adefer("groups", "user_permissions").aget(pk=user_id)

OR

use 2nd solution from my previous comment.

I'm closing this issue. But feel free to re-open if you continue to face it :)

from supertokens-python.

itsjef avatar itsjef commented on September 15, 2024

Hi @KShivendu, thank you for your quick reply.

In your first comment's first code snippet, you used except Exception therefore it would catch SynchronousOnlyOperation and, as a result, your get_all_permissions would always returns [].

The second snippet still cause the same error. Looks like there is no difference between using sync_to_async as a decorator versus a wrapper.


And in your second comment, my apologies for copying the wrong traceback. My User model inherits PermissionsMixin so I theorized the m2m relationships could have created a join query and wouldn't work asynchronously so I added defer.

Anyway, the SynchronousOnlyOperation exception happened with or without it.

from supertokens-python.

itsjef avatar itsjef commented on September 15, 2024

Other query versions I have tried are:

user = UserModel.objects.all().get(pk=user_id)

user = UserModel.objects.all().filter(pk=user_id).get()

user = UserModel.objects.all().filter(pk=user_id).first()

user = list(UserModel.objects.all().filter(pk=user_id))[0]  # as suggested in: https://stackoverflow.com/a/68760464

(notes that all() and filter(...) can be interchanged)

from supertokens-python.

itsjef avatar itsjef commented on September 15, 2024

Please help re-open the issue since it is still unresolved. I don't have the permission to do so.

from supertokens-python.

KShivendu avatar KShivendu commented on September 15, 2024

except Exception therefore it would catch SynchronousOnlyOperation and, as a result, your get_all_permissions would always returns []

Ahh good point. Actually, I wanted to be able to catch all the errors so I used Exception. But the thing is, I ran the debugger as well and my query was giving me the output as I expected. Plus, the end result wasn't [] but ["foo"].

Can you try replicating this issue in a cloud environment like Repl.it or gitpod?

from supertokens-python.

KShivendu avatar KShivendu commented on September 15, 2024

Nice. Thanks for creating the repo :)

from supertokens-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.