Comments (9)
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.
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.
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.
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.
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.
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.
Please help re-open the issue since it is still unresolved. I don't have the permission to do so.
from supertokens-python.
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.
Nice. Thanks for creating the repo :)
from supertokens-python.
Related Issues (20)
- supertokens FastAPI middleware uses BaseHTTPMiddleware which breaks async/background tasks HOT 7
- Switch away use of urllib.request HOT 2
- Python Flask @app.before_request avoid next function process HOT 4
- Upgrade cryptography library HOT 4
- Upgrade httpx HOT 1
- Regression: core version is not compatible with this FastAPI SDK HOT 3
- tldextract library isn't handling ec2-xx-yyy-zzz-0.compute-1.amazonaws.com HOT 1
- signOut not revoking session HOT 3
- Google Social Login: jwt.exceptions.ImmatureSignatureError: The token is not yet valid (iat) HOT 12
- Multiple `Set-Cookie` headers for the `sAccessToken` cookie
- Flask does not save user context across API lifecycle
- SECURITY_ISSUE : Http request context (response-headers) not getting popped up from the global flask context on request completion HOT 4
- 200 HTTP code on error HOT 1
- httpx 0.26 HOT 1
- FastAPI setup issue HOT 6
- "User cannot be found" on dashboard for 3rd party users HOT 1
- Github thirdparty provider does not set raw_user_info_from_provider in get_user_info HOT 1
- Celery integration HOT 1
- Update how fake emails are generated HOT 2
- Update aiosmtplib HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from supertokens-python.