supertokens-python icon indicating copy to clipboard operation
supertokens-python copied to clipboard

[#501] handle sync when uvloop.Loop is running

Open mavwolverine opened this issue 1 year ago • 8 comments

Summary of change

(A few sentences about this PR)

Related issues

  • #501

Test Plan

(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work. Bonus points for screenshots and videos!)

Documentation changes

(If relevant, please create a PR in our docs repo, or create a checklist here highlighting the necessary changes)

Checklist for important updates

  • [x] Changelog has been updated
  • [ ] coreDriverInterfaceSupported.json file has been updated (if needed)
    • Along with the associated array in supertokens_python/constants.py
  • [ ] frontendDriverInterfaceSupported.json file has been updated (if needed)
  • [ ] Changes to the version if needed
    • In setup.py
    • In supertokens_python/constants.py
  • [ ] Had installed and ran the pre-commit hook
  • [ ] Issue this PR against the latest non released version branch.
    • To know which one it is, run find the latest released tag (git tag) in the format vX.Y.Z, and then find the latest branch (git branch --all) whose X.Y is greater than the latest released tag.
    • If no such branch exists, then create one from the latest released branch.
  • [ ] If have added a new web framework, update the supertokens_python/utils.py file to include that in the FRAMEWORKS variable
  • [ ] If added a new recipe that has a User type with extra info, then be sure to change the User type in supertokens_python/types.py
  • [ ] Make sure that syncio / asyncio functions are consistent.
  • [ ] If access token structure has changed
    • Modified test in tests/sessions/test_access_token_version.py to account for any new claims that are optional or omitted by the core

Remaining TODOs for this PR

  • [ ] Item1
  • [ ] Item2

mavwolverine avatar May 11 '24 06:05 mavwolverine

Thanks @virajkanwade , this PR needs to add tests:

  • Unit tests
  • update the existing e2e test server to test this change, or create a new e2e test server that runs fastapi in this mode.
  • Pre commit hook PR check is failing, so that needs fixing too.

One question: In our e2e test, we already run fastapi server using uvicorn app:app, and it works fine (see here). So i wonder why you ran into an issue?

rishabhpoddar avatar May 11 '24 15:05 rishabhpoddar

One question: In our e2e test, we already run fastapi server using uvicorn app:app, and it works fine (see here). So i wonder why you ran into an issue?

something very specific to how I am setting it up

def create_app():
    # $ export SUPERTOKENS_NEST_ASYNCIO=1
    sync(supertokens_init())
    sync(supertokens_multitenancy_setup_personal())
    sync(supertokens_multitenancy_setup_school())

    app = FastAPI()
    app.add_middleware(get_middleware())

where the multitenancy setup functions call create_or_update_tenant and create_or_update_third_party_config.

mavwolverine avatar May 11 '24 17:05 mavwolverine

What are the import statements that you have used for the functions from our lib?

rishabhpoddar avatar May 12 '24 12:05 rishabhpoddar

What are the import statements that you have used for the functions from our lib?

I had tried

from supertokens_python.recipe.multitenancy.syncio import (
    create_or_update_tenant,
    create_or_update_third_party_config,
)

mavwolverine avatar May 12 '24 13:05 mavwolverine

Right. So why do you need to wrap them with sync()?

rishabhpoddar avatar May 12 '24 14:05 rishabhpoddar

Right. So why do you need to wrap them with sync()?

The syncio functions gave the "loop already running" error.

So I followed the code to see the difference between syncio and asyncio functions, and found supertokens sync function.

Then did the whol research above and created my own sync function, hence the sync calls you are seeing in the snippet.

mavwolverine avatar May 12 '24 16:05 mavwolverine

One question: In our e2e test, we already run fastapi server using uvicorn app:app, and it works fine (see here). So i wonder why you ran into an issue?

@rishabhpoddar If I run it as

uvicorn --factory main:create_app

I get

RuntimeError: this event loop is already running.

If I run it as

if __name__ == '__main__':
    import uvicorn

    uvicorn.run(
        create_app(),
    )

python main.py

it seems to work with the supertokens syncio functions

mavwolverine avatar May 12 '24 16:05 mavwolverine

I see. Okay! Thanks for the info. We can continue working on this PR, in a few weeks, to add more tests (as described in my previous comment).

rishabhpoddar avatar May 12 '24 16:05 rishabhpoddar

Not merging this. The right way to use it is described in this issue - https://github.com/supertokens/supertokens-python/issues/501

sattvikc avatar May 23 '24 07:05 sattvikc