Skip to content

fix(asgi): isolate pipelined HTTP/1.1 requests from previous request scope - #8331

Open
TejasLamba2006 wants to merge 1 commit into
getsentry:masterfrom
TejasLamba2006:fix/asgi-pipelined-isolation-scope
Open

TejasLamba2006 wants to merge 1 commit into
getsentry:masterfrom
TejasLamba2006:fix/asgi-pipelined-isolation-scope

Conversation

@TejasLamba2006

Copy link
Copy Markdown

Description

Under ASGI servers such as uvicorn, a queued pipelined HTTP/1.1 request task is scheduled via loop.create_task() from on_response_complete() inside the previous request's final await send(...), before SentryAsgiMiddleware._run_app's finally block exits. As a result, the second request's task inherited _asgi_middleware_applied = True (taking the recursive nested-middleware fast path with no transaction or ASGI event processor) as well as the previous request's active Scope and isolation Scope (leaking user, tags, breadcrumbs, and trace context across requests).

  • Replace the boolean _asgi_middleware_applied flag with a per-request _RequestState storing parent_scope, parent_isolation_scope, and response_complete.
  • Mark request_state.response_complete = True in _sentry_wrapped_send right before forwarding the final http.response.body event (more_body falsy), and reset the ContextVar via token in finally.
  • Treat SentryAsgiMiddleware as nested only when request_state is present and not request_state.response_complete; when response_complete is True (pipelined sibling task), enter parent_scope and parent_isolation_scope before forking a fresh sentry_sdk.isolation_scope() so pre-request ambient tags still carry over without leaking request-scoped state.
  • Add regression tests in tests/integrations/asgi/test_asgi.py for pipelined send() task spawning and nested SentryAsgiMiddleware layers (294 passed across ASGI, Starlette, and FastAPI suites).

Issues

uvicorn starts the next pipelined request's task from inside the final
send() of the previous one, so the new task inherited a context in which
the previous request was still marked as active. The middleware then took
the nested fast path and skipped creating an isolation scope and a
transaction, and anything it did run in the previous request's scopes.

Track a per-request state in the context variable that is flagged once
the final response body has been sent. A middleware that finds a
completed request's state starts a fresh request from the scopes the
previous request started from. Nested middleware inside an in-flight
request still takes the fast path.

Fixes getsentry#8271
@TejasLamba2006
TejasLamba2006 requested a review from a team as a code owner November 18, 2026 15:26

This branch has not been deployed

No deployments
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 this pull request may close these issues.

ASGI: a pipelined HTTP/1.1 request under uvicorn runs in the previous request's isolation scope

1 participant