Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context overwritten on non-async API route #146

Open
Bharat23 opened this issue Jan 27, 2025 · 1 comment
Open

Context overwritten on non-async API route #146

Bharat23 opened this issue Jan 27, 2025 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@Bharat23
Copy link

I am using this in fastAPI and when I use this in a route that doesn't use async keyword the context is overwitten by the call that responded faster.

from starlette_context import context
from fastapi import Query
from time import sleep


### Steps to reporoduce

@app.get("/")
def health(value_to_set: str = Query(), time_to_sleep: int = Query()):
    context.some_value = value_to_set
    sleep(time_to_sleep)
    return context.some_value

Call these in parallel with one worker

curl /?value_to_set=a&time_to_sleep=10

output:
b


curl /?value_to_set=b&time_to_sleep=2

output:
b

Middleware is already setup as per the docs. Also, using the async keyword resolves the issue.

I am not sure if I am missing something or is this a bug. Any help on this appreciated.

@tomwojcik tomwojcik assigned tomwojcik and unassigned tomwojcik Mar 3, 2025
@tomwojcik tomwojcik added the help wanted Extra attention is needed label Mar 3, 2025
@tomwojcik
Copy link
Owner

Great question. Sadly, I have no idea either. IIRC sync requests are run in a separate thread and I found this. encode/starlette#1258

Contributions welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants