fastapi_msal icon indicating copy to clipboard operation
fastapi_msal copied to clipboard

Not working for multiple workers by uvicorn

Open maveius opened this issue 10 months ago • 3 comments

Describe the bug Did anyone tried to use it for multiple workers >1 like 4 ? uvicorn app.main:app --host 0.0.0.0 --port 8001 --workers 4 When multiple workers by uvicorn I think that different sub-processes are inside and session is not shared between workers (processes) then issue like detail "AADSTS501481: The Code_Verifier does not match the code_challenge supplied in the authorization request. Trace ID:..."

To Reproduce Steps to reproduce the behavior:

  1. Run in docker by uvicorn app with workers 4 uvicorn app.main:app --host 0.0.0.0 --port 8001 --workers 4
  2. Click on login button
  3. Wait for result
  4. See error "AADSTS501481: The Code_Verifier does not match the code_challenge supplied in the authorization request. Trace ID:..."

Expected behavior It should not depend on which worker has this code in memory in one single session, but should share between processes.

Environment Settings

  • OS: Linux / Docker (python:3.12)
  • Python Version: 3.12
  • Package Versions:
  • msal: 1.32.3
  • fastapi: 0.115.13
  • fastapi_msal: 2.1.7

Additional context

maveius avatar Jun 24 '25 12:06 maveius

@maveius - this is correct, but for this there should be a support for a file based / external session management like redis. Was on my roadmap but didn't got there. If required we can look into that together.

dudil avatar Jul 06 '25 08:07 dudil

@dudil is there a good reason why it cannot be implemented stateless? if it can be we're happy to look into it with you :)

maciejmochol avatar Jul 24 '25 08:07 maciejmochol

I would like to have this resolved as well. @dudil , @maveius , I propose to use diskcache (https://pypi.org/project/diskcache/) for this: performant and no dependency on an external server. Would that be a good option for you as well?

tjadevries avatar Oct 03 '25 08:10 tjadevries

@dudil Let me know your preferred means of session storage, and I can look into this. Normally I'd go with redis, but if you have a preference, I'm happy to look at implementation.

nathanrhart avatar Feb 17 '26 13:02 nathanrhart

@dudil Let me know your preferred means of session storage, and I can look into this. Normally I'd go with redis, but if you have a preference, I'm happy to look at implementation.

I am interested in this as well. But maybe it could have database interface as well?

VictorErmakov avatar Feb 20 '26 14:02 VictorErmakov