The Fix
Upgrade to version 0.21.1 or later.
Based on closed Kludex/uvicorn issue #1902 · PR/commit linked
@@ -201,7 +201,7 @@ def get_connected_protocol(
config=config,
server_state=server_state,
- app_state=lifespan.state.copy(),
+ app_state=lifespan.state,
_loop=loop,
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),])
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Option A — Upgrade to fixed release\nUpgrade to version 0.21.1 or later.\nWhen NOT to use: Do not use this fix if you require persistent state across requests.\n\n
Why This Fix Works in Production
- Trigger: Unexpected behavior where request state modifications outlive the request lifecycle.
- Mechanism: Reset lifespan state on each request to prevent modifications to 'request.state' from persisting between requests.
- Why the fix works: Reset lifespan state on each request to prevent modifications to 'request.state' from persisting between requests. (first fixed release: 0.21.1).
- If left unfixed, this can cause silent data inconsistencies that propagate (bad cache entries, incorrect downstream decisions).
Why This Breaks in Prod
- Shows up under Python 3.8.10 in real deployments (not just unit tests).
- Production symptom (often without a traceback): Unexpected behavior where request state modifications outlive the request lifecycle.
Proof / Evidence
- GitHub issue: #1902
- Fix PR: https://github.com/kludex/uvicorn/pull/1903
- First fixed release: 0.21.1
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.95
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.63
Verified Execution
We executed the runnable minimal repro in a temporary environment and captured exit codes + logs.
- Status: PASS
- Ran: 2026-02-11T16:52:29Z
- Package: uvicorn
- Fixed: 0.21.1
- Mode: fixed_only
- Outcome: ok
Logs
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“### Discussed in https://github.com/encode/uvicorn/discussions/1901 <div type='discussions-op-text'> <sup>Originally posted by **yromanyshyn** March 16, 2023</sup> support of the lifespan state and the notion of the app_state was added to t”
Failure Signature (Search String)
- Unexpected behavior where request state modifications outlive the request lifecycle.
Copy-friendly signature
Failure Signature
-----------------
Unexpected behavior where request state modifications outlive the request lifecycle.
Error Message
Signature-only (no traceback captured)
Error Message
-------------
Unexpected behavior where request state modifications outlive the request lifecycle.
Minimal Reproduction
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),])
Environment
- Python: 3.8.10
What Broke
Unexpected behavior where request state modifications outlive the request lifecycle.
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
Upgrade to version 0.21.1 or later.
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Option D — Guard side-effects with OnceOnly Guardrail for side-effects
Mitigate duplicate external side-effects under retries/timeouts/agent loops by gating the operation before calling external systems.
- Place OnceOnly between your code/agent and real side-effects (Stripe, emails, CRM, APIs).
- Use a stable key per side-effect (e.g., customer_id + action + idempotency_key).
- Fail-safe: configure fail-open vs fail-closed based on blast radius and spend risk.
- This does NOT fix data corruption; it only prevents duplicate side-effects.
Show example snippet (optional)
from onceonly import OnceOnly
import os
once = OnceOnly(api_key=os.environ["ONCEONLY_API_KEY"], fail_open=True)
# Stable idempotency key per real side-effect.
# Use a request id / job id / webhook delivery id / Stripe event id, etc.
event_id = "evt_..." # replace
key = f"stripe:webhook:{event_id}"
res = once.check_lock(key=key, ttl=3600)
if res.duplicate:
return {"status": "already_processed"}
# Safe to execute the side-effect exactly once.
handle_event(event_id)
Fix reference: https://github.com/kludex/uvicorn/pull/1903
First fixed release: 0.21.1
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Do not use this fix if you require persistent state across requests.
- Do not use this to hide logic bugs or data corruption. Use it to block duplicate external side-effects and enforce tool permissions/spend caps.
Verify Fix
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Did This Fix Work in Your Case?
Quick signal helps us prioritize which fixes to verify and improve.
Prevention
- Capture the exact failing error string in logs and tests so you can reproduce via a minimal script.
- Pin production dependencies and upgrade only with a reproducible test that hits the failing path.
Version Compatibility Table
| Version | Status |
|---|---|
| 0.21.1 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.