Hello, I'm using supertokens within my flask appli...
# support-questions-legacy
j
Hello, I'm using supertokens within my flask application using https://github.com/supertokens/supertokens-python This is a short example of my code: from supertokens_python.framework.flask import Middleware app = Flask(__name__) init_supertokens(app) Middleware(app) CORS(app=app, origins=[app.config.get('WEBSITE_DOMAIN', '')], supports_credentials=True, allow_headers=["Content-Type"] + get_all_cors_headers()) I was trying to add flask_socketio using the following code:
from flask_socketio import SocketIO
SocketIO(app)
But than I came across asyncio issues(Added image of the error) It seems that there is a clash between the socket io event loop and the supertokens asyncio, can you please recommend the best practice of how to integrate flask_socketio while I already have supertokens installed on my flask? Thanks!
@rp_st
r
@KShivendu can help with this
k
@justauser6119 I'll try to replicate this and get back to you in 10-15mins 🙂
j
Thanks!
k
Hi @justauser6119 I just noticed your error is KeyError and doesn't seem related to flask_socketio or supertokens?
also what triggers this error for you? my application was up and running without any error.
j
@KShivendu So i'm using socketio and http handlers in my flask.(I think that that's what triggered it - you can try and create http endpoint and socket endpoint). This is the specific exception I get: Exception ignored in: Traceback (most recent call last): File "/usr/local/Cellar/python@3.9/3.9.17/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 688, in __del__ self.close() File "/usr/local/Cellar/python@3.9/3.9.17/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/unix_events.py", line 60, in close for sig in list(self._signal_handlers): AttributeError: '_UnixSelectorEventLoop' object has no attribute '_signal_handlers' /Users/omriben-shoham/Desktop/code/GenWayWebsite/backend/app/routes/meet.py:169: RuntimeWarning: coroutine 'get_session' was never awaited pass RuntimeWarning: Enable tracemalloc to get the object allocation traceback
k
> routes/meet.py:169: RuntimeWarning: coroutine 'get_session' was never awaited Are you not doing something like
s = await get_session()
?
j
It's a bit confusing, let me try to organize that situation. I have flask application, with combination of websockets and rest api Part of the rest api using
@verifySession
decorator (supplied by you) While the websocket has no any decorator
@KShivendu Any help here? 🙂
@KShivendu I'll show you the specific usage where the error occur: I'm importing this: from supertokens_python.recipe.session.syncio import get_session Than I have some http endpoint which doing this: def some_endpoint(): session = get_session(request=request, session_required=False) This is the get_session which raise the failure
@rp_st any help here?🙏
k
@justauser6119 this doesn't add up. are you sure you're getting:
Copy code
/Users/omriben-shoham/Desktop/code/GenWayWebsite/backend/app/routes/meet.py:169: RuntimeWarning: coroutine 'get_session' was never awaited
when importing from syncio?
j
Yep this is my import: from supertokens_python.recipe.session.syncio import get_session and this is my usage: try: session = get_session(request=request, session_required=False) if (session): user_id = session.get_user_id() except Exception as e: pass and this is my error: Exception ignored in: Traceback (most recent call last): File "/usr/local/Cellar/python@3.9/3.9.17/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 688, in __del__ self.close() File "/usr/local/Cellar/python@3.9/3.9.17/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/unix_events.py", line 60, in close for sig in list(self._signal_handlers): AttributeError: '_UnixSelectorEventLoop' object has no attribute '_signal_handlers' /Users/omriben-shoham/Desktop/code/GenWayWebsite/backend/app/routes/meet.py:337: RuntimeWarning: coroutine 'get_session' was never awaited
@KShivendu
@KShivendu Its worth to mention that this error raised since im running my flask with the socketio: socketio.run(host='0.0.0.0', port=8000, app=app)
k
is this part of meet.py?
asking since (afaik) there's no way to face get_session was never awaited with asyncio.
j
Yep it's part of my code, if you will try to create simple flask and wrap it with socket-io and than call your syncio get_session function I believe you will get the same problem. BTW, when I look deeper I see that my code reach to this function: https://github.com/supertokens/supertokens-python/blob/8b0cb9103d63b3cd81cba9d5cecc0bad122fa3ca/supertokens_python/async_to_sync_wrapper.py#L23 and then get the following error: {TypeError}TypeError('changelist must be an iterable of select.kevent objects')
So im not specifically asking to not wait for get_session I think it's happen becuase of the error I sent you now @KShivendu
k
Okay. Let me check.
j
@KShivendu Ok so i found out what was the problem, I originally insitialized my flask socket io this way: socketio.init_app(app, cors_allowed_origins="*",) And the default async mode if this is 'threading' and than it clashed with supertokens async mode which is gevent if I understand right. So all I have to do is the following: socketio.init_app(app, cors_allowed_origins="*", async_mode='gevent') And than the errors gone.
k
okay. how were you running the application?
https://gist.github.com/KShivendu/feffa6be4806d76fd95c86734545aba4 I just finished replicating your issue. But it worked seamlessly for me.
j
I've looked on your gist, I'm wondering how do you run the code? I'm runing with python main.py @KShivendu
k
same. I just did
python main.py
j
Is it possible to jump on a call and I'll show you exactly what I'm facing?
k
okay.
Let's join the voice channel - general.
14 Views