Hey, I've this error Please advise Using Flask, with SQLAlchemy ``` /usr/local/lib/python3.9/site-pa...
a
Hey, I've this error Please advise Using Flask, with SQLAlchemy
Copy code
/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/context.py:2695: RuntimeWarning: coroutine 'Supertokens.middleware' was never awaited
  _instance = loading._instance_processor(
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Copy code
ERROR:APP_NAME:Exception on /case/ [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.9/site-packages/flask_cors/extension.py", line 176, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1482, in full_dispatch_request
    rv = self.preprocess_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1974, in preprocess_request
    rv = self.ensure_sync(before_func)()
  File "/usr/local/lib/python3.9/site-packages/supertokens_python/framework/flask/flask_middleware.py", line 54, in _
    result: Union[BaseResponse, None] = sync(st.middleware(request_, response_))
  File "/usr/local/lib/python3.9/site-packages/supertokens_python/async_to_sync_wrapper.py", line 33, in sync
    return loop.run_until_complete(co)
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 623, in run_until_complete
    self._check_running()
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 583, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
r
hey @amagic5502 how have you added our middleware into your app?
a
Yup, using @verify_session middleware
r
what about the supertokens middleware that exposes all of our routes?
a
Yup, also on the init app
Copy code
from supertokens_python.framework.flask import Middleware

  Middleware(app)
r
hmm.
@KShivendu can help here.
a
@rp_st Whats the middle is used for? could I just remove the usage of it?
r
it's used for exposing all the auth routes to the frontend
like sign in, sign up etc
so you should keep it
a
๐Ÿ˜ฆ It ruins my production env
It never happend before
I really need your help guys
r
yea.. must be something silly. @KShivendu can help when he is available.
a
To put more information, I'm using
gunicorn
with
geventwebsocket.gunicorn.workers.GeventWebSocketWorker
on production env
k
Hi @amagic5502 let me take a look.
a
@KShivendu Thanks!
If you would like we could jump on a call
r
calls are reserved only for premium support.
k
@amagic5502 it seems to be working fine for me. can you share an example repo/gist to replicate this issue?
a
Are you working with
gunicorn
with this executor?
geventwebsocket.gunicorn.workers.GeventWebSocketWorker
k
Yup
a
I mean, I couldn't really outsource my code. I understood that @rp_st wants only premium users to have calls with you guys' developers. I have no other solution. If thats okay with @rp_st to have a call this time, it will be helpful.
r
we will try and help over chat here ๐Ÿ™‚
can you upload an example app that can reproduce this issue?
a
I'll try.
init_app.py
Copy code
from flask import Flask
from flask_cors import CORS
from dotenv import load_dotenv

from flask_sqlalchemy import SQLAlchemy
from flask_socketio import SocketIO
from sqlalchemy import create_engine
from supertokens_python import get_all_cors_headers

from app.authentication import init_supertokens
from supertokens_python.framework.flask import Middleware

from app.metrics import FlaskMetrics
from app.clients.posthog import PostHogClient
from utils.server import is_gunicorn

load_dotenv()

app = Flask("app")
socketio = SocketIO()
db = SQLAlchemy()
metrics = FlaskMetrics()
posthog_client = PostHogClient()


def create_app():
    app.config.from_object('app.config.Config')

    db.init_app(app)
    init_supertokens(app)
    posthog_client.init_app(app)
    metrics.init_app(app)

    Middleware(app)

    CORS(app=app,
         origins=[app.config.get('WEBSITE_DOMAIN', '')],
         supports_credentials=True,
         allow_headers=["Content-Type"] + get_all_cors_headers())

    async_mode = 'gevent' if is_gunicorn() else 'threading'

    socketio.init_app(app, cors_allowed_origins="*", async_mode=async_mode)

    with app.app_context():
        from app.routes import register_routes
        register_routes(app)

        create_db_if_not_exists(db.engine)
        db.create_all()

    return app, socketio


def create_db_if_not_exists(engine):
    from sqlalchemy_utils import database_exists, create_database

    if not database_exists(engine.url):
        create_database(engine.url)
main.py
Copy code
@any_bp.route('/', methods=['GET'])
@verify_session()
def get_database_entity_route():
    return jsonify(get_database_entity) // Any database call
run this command
Copy code
gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker main:app -w 1 -b 0.0.0.0:8000
r
@KShivendu - please try
k
Hi. This isn't suffucient to replicate the issue. I'm getting too many errors.
a
@KShivendu Just ignore the things that are not relevant. whats relevant is that Im using Flask with Socketio, hosted on Gunicorn WSGI and have a route that call to DB
k
@amagic5502 I tried commenting/removing irrelevant stuff but iirc I still faced multiple issues.
r
@amagic5502 would be good if you can provide a full example on github that we can just run
otherwise, we may not be able to help soon
a
Guys, I feel that we are a bit stuck and lost here. I feel that the documentation is missing for websockets and async application support and thats why I'm facing all of these issues. What could help here is a 1:1 20minutes session with one of my developers. I didn't find the pricing plan for premium users, but I feel the solution above will close the loop.
r
we do have docs for async usage. You need to make sure you have the correct mode set when calling the init function
i have DM'd you the pricing for support
a
Could you please attach here the reference? I couldn't find it
r
which recipe are you using?
a
Password + Social login
under the fastai code tab, it has the setting of
mode='asgi'
.
You need to set it to
wsgi
if using
gunicorn
a
The thing is we are using GUNICORN + FLASK + SOCKETIO Not FastAPI as mentioned
r
right, so set he mode to wsgi in flask in the same way
a
Isn't it the default?
r
im not sure. You could try to set it explicilty and check if that works
a
Let's try
will update soon
r
if it doesn't work, i really encourage you to send us a demo app that compiles soon and can reliably reproduce the issue that you are facing
and if you can't reliably reproeuce the issue, then maybe open a github issue about it. If enough people upvote / complain about the same thing, we can have a look
a
I'm trying.. thats really hard. we have many dependencies, worked about 3 hours about that..
r
cause we are not able to reproduce this issue on our end (and we did try)
a
The wsgi mode didn't work. Keep working on creating gist..
r
maybe the issue is that we only work with sync flask and not asyncio flask. Are you using asyncio flask?
a
No, sync flask + socketio-flask
r
hmm.
Then we can't reproduce it
see if you can provide a demo app
a
Trying my best, believe me. production app is got about 5% failed requests due to this problem
r
could be that adding socketio is causing the issue somehow?
a
Yup. it is
r
> production app is got about 5% failed requests due to this problem This means that it's working 95% of the time?
a
Yup..
Thats why it's so hard to reproduce
r
i see.
which version of our python sdk are you using?
a
Copy code
Name: supertokens-python
Version: 0.14.8
r
thanks
a
Please notice that I'm using GEVENT as the
async_mode
when running on GUNICORN https://discord.com/channels/603466164219281420/1145626044393594880/1145641061348286536
r
do you think using this lib may be the solution? https://pypi.org/project/nest-asyncio/
and we use
asyncio
.
Do you think if we use
nest-asyncio
, this problem will be fixed?
a
r
hmm
a
It seems that it should be the solution, but I'm afraid you guys to put much effort and then we will figure it out thats not the solution. is there any lean way I can check it that works?
r
i could create a fork of the repo that we have, switch asyncio with this and you could use the forked version. See if that solves the issue - how does that sound?
a
Sounds great
r
ok. Let me see what i can do.
a
Let's do it
r
you can try it out using:
Copy code
pip install git+https://github.com/supertokens/supertokens-python.git@b4b0c4a8ad52838c7f39a5d2167e09aedc8580ac --force
We are still running our tests on it, and it should take 1-2 hours
a
I'm getting that error
r
this seems to be a totally unreleated issue
an issue when we import from httpx
a
What are the next action items from my side? ๐Ÿ™‚
r
im not sure why you are getting this issue
it's coming from httpx library
and i didn't change that in this new commit at all
so..
did anything else change on your end?
try with this commit hash ->
4d20336929ce23689f27e39992d5316522c96d15
Copy code
pip install git+https://github.com/supertokens/supertokens-python.git@4d20336929ce23689f27e39992d5316522c96d15 --force
a
Nothing changed from my side ๐Ÿ˜ฎ
The commit has also not working. let me try to revert to 0.14.8
r
yup
did using 0.14.8 solve the above error?
a
Hey, after installing the github version, I'm having issues with the python env, trying to restart it.
r
hm ok
a
Managed to install
This is the error I'm facing right now
@rp_st That happens sometimes, not always
r
Hmm. Then maybe the change I did isnโ€™t enough. Iโ€™ll check in a bit
a
BTW, it seems related to
get_user_by_id
from syncio
The previous error is not reproduced, which is great
Copy code
from supertokens_python.recipe.thirdpartyemailpassword.syncio import get_user_by_id
@rp_st The error above actually not reproduced.. it feels like you solved the issue!
I've ran about 200 tests ๐Ÿ˜›
It happened only once (EDITED BELOW)
Actually it happened again few seconds ago ๐Ÿ˜ฆ
Seems like the application is freezing when it happens
r
hmm
it happens when you call
get_user_by_id
?
can i see how you are calling it?
a
Yup sure
Copy code
def validate_is_admin():
    def decorator(route_function):
        @wraps(route_function)
        def wrapper(*args, **kwargs):
            session = g.supertokens
            user_id = session.get_user_id()
            user = get_user_by_id(user_id)

            if (not user):
                abort(401)
                
            if user.email.endswith('@') and user.third_party_info:
                return route_function(*args, **kwargs)
            abort(403)

        return wrapper

    return decorator
r
ok im trying another change
see if that works. 1 min
try with this commit hash ->
d900f065687445c058131b859b00b34143e3248b
Copy code
pip install git+https://github.com/supertokens/supertokens-python.git@d900f065687445c058131b859b00b34143e3248b --force
a
Checking
Same error
Copy code
File "/opt/homebrew/lib/python3.9/site-packages/supertokens_python/recipe/thirdpartyemailpassword/asyncio/__init__.py", line 29, in get_user_by_id
    return await ThirdPartyEmailPasswordRecipe.get_instance().recipe_implementation.get_user_by_id(
  File "/opt/homebrew/lib/python3.9/site-packages/supertokens_python/recipe/thirdpartyemailpassword/recipeimplementation/implementation.py", line 134, in get_user_by_id
    tp_user = await self.tp_get_user_by_id(user_id, user_context)
  File "/opt/homebrew/lib/python3.9/site-packages/supertokens_python/recipe/thirdparty/recipe_implementation.py", line 36, in get_user_by_id
    response = await self.querier.send_get_request(
  File "/opt/homebrew/lib/python3.9/site-packages/supertokens_python/querier.py", line 145, in send_get_request
    return await self.__send_request_helper(path, "GET", f, len(self.__hosts))
  File "/opt/homebrew/lib/python3.9/site-packages/supertokens_python/querier.py", line 298, in __send_request_helper
    raise_general_exception(e)
  File "/opt/homebrew/lib/python3.9/site-packages/supertokens_python/exceptions.py", line 25, in raise_general_exception
    raise GeneralError(msg) from None
supertokens_python.exceptions.GeneralError: unknown async library, or not in async context
@rp_st how do I ensure the version of the package?
r
Did pip freeze and check that supertokens-python has the same commit hash as what you installed last
ok i've made another change
try:
Copy code
pip install git+https://github.com/supertokens/supertokens-python.git@dbca4da04d3648143db567e6d327c6423593bdd9 --force
Iโ€™d be ok with getting on a call here to help you solve this issue, and to help us figure out why this is happening.
a
No, still ๐Ÿ˜ฆ
Verified I'm with the the right version
Copy code
backend git:(main) pip freeze  | grep super
supertokens-python @ git+https://github.com/supertokens/supertokens-python.git@dbca4da04d3648143db567e6d327c6423593bdd9e
r
@KShivendu will help here. If needed, we can arrange a call
a
@rp_st I'm here if you need me ๐Ÿ™‚
r
I think @KShivendu is able to replicate the issue. So Iโ€™ll let him take care of this.
a
Thats great. thanks. Looking forward for your update.
Any update? @KShivendu
r
He will get back on Monday.
a
Got you. Do you think it is worth changing the version in PRODUCTION meanwhile to the committed version you sent me? @rp_st
r
I donโ€™t think so.
a
Got you. Is there any way I can help to promote this issue? as I mentioned before it fails 5% of our production requests
r
We will have a look at it tomorrow for sure.
k
@amagic5502 do you see the error more than once in a single session after using the nest_asyncio branch that @rp asked you to use? (here session means you running
gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 -b 0.0.0.0:3001 app:app
)
a
@KShivendu yup
k
Well. I'm getting the error at max once per session.
a
Let me recheck @KShivendu
@KShivendu I think it's not related since it happens even before upgrading the version, when trying to login to dev supertokens I'm getting this error
Copy code
error Error: SuperTokens core threw an error for a GET request to path: '/apiversion' with status code: 502 and message: <html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>
Even in your Admin panel
I've seen you have incident in dev env - https://supertokens.instatus.com/clm4ikl7n43588biom5hvzeppv
@KShivendu After resolving the dev env, it actually looks like the error happens once - as you mentioned. What are the next steps?
k
@amagic5502 that's good news then. I'm trying to create a minimalistic app to isolate the exact cause of this.
a
@KShivendu In terms of ETA, how long do you think it will take to release the current version (with the single error) to production?
r
You could use the version from the commit hash if you like
we will only be releasing it once we are 100% sure why the issue happens and have fixed it, so no promise on timeline
a
Yup sure, I'm asking for an estimated time (days/weeks( to understand how to tackle this issue on our side.
r
Really not sure. We are trying to fix it today itself. But depends on the actual issue. Maybe a week.
a
Sounds great. thanks for the update. I'll use the commit you shared with me in production, it will be available permanently, right?
r
It should be. But i'll make sure to message you whenever we are merging from that branch, or anything. Can you dm me your email, in case you don't reply on discord or this thread disappears.
a
Yup sure
r
actually, it would be best if you fork the python repo, and then use your forked version in your prod code
this way, there is 0 chances for the commit going missing from our end
a
Thats a great suggestion. will do it. thanks @rp_st
r
hey @amagic5502
we have added a new commit which should resolve the first error you get as well. You can update your forked repo on the branch of
using-nest-asyncio
and try to use that. This is the commit hash ->
7c74be58df725168bbecaf466404e3aa0f4db967
a
Due to higher priorities I won't be able to reach right now. In two weeks I'll test it and let you know.
r
sounds good.
25 Views