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

with 'uvicorn==0.21.0' modifications made to the 'request.state' persist between requests #1902

Closed
Kludex opened this issue Mar 16, 2023 Discussed in #1901 · 0 comments · Fixed by #1903
Closed

with 'uvicorn==0.21.0' modifications made to the 'request.state' persist between requests #1902

Kludex opened this issue Mar 16, 2023 Discussed in #1901 · 0 comments · Fixed by #1903

Comments

@Kludex
Copy link
Member

Kludex commented Mar 16, 2023

Discussed in #1901

Originally posted by yromanyshyn March 16, 2023
support of the lifespan state and the notion of the app_state was added to the latest release of the uvicorn, as result, now changes made to the request.state persist between requests (actually in a very weird way/), IMO, it is confusing and unexpected behavior, you do not expect that changes made to the request state will outlive the request/

Code snippet:

from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette.routing import Route

def handler(request):
    if not hasattr(request.state, "foo"):
        request.state.foo = 10
    else:
        request.state.foo += 1
    return PlainTextResponse(f"Foo: {request.state.foo}")

app = Starlette(debug=True, routes=[Route('/', handler),])

Demonstration:
(please, look the video to the end)

simplescreenrecorder-.3.mp4

Installed packages and python version:

>> pip freeze
anyio==3.6.2
click==8.1.3
h11==0.14.0
idna==3.4
sniffio==1.3.0
starlette==0.26.1
typing-extensions==4.5.0
uvicorn==0.21.0

>> python --version
Python 3.8.10
```</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant