Repository navigation
fix(asgi): isolate pipelined HTTP/1.1 requests from previous request scope - #8331
Open
TejasLamba2006 wants to merge 1 commit into
Open
TejasLamba2006 wants to merge 1 commit into
TejasLamba2006 wants to merge 1 commit into
Conversation
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
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Under ASGI servers such as
uvicorn, a queued pipelined HTTP/1.1 request task is scheduled vialoop.create_task()fromon_response_complete()inside the previous request's finalawait send(...), beforeSentryAsgiMiddleware._run_app'sfinallyblock 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 activeScopeand isolationScope(leaking user, tags, breadcrumbs, and trace context across requests)._asgi_middleware_appliedflag with a per-request_RequestStatestoringparent_scope,parent_isolation_scope, andresponse_complete.request_state.response_complete = Truein_sentry_wrapped_sendright before forwarding the finalhttp.response.bodyevent (more_bodyfalsy), and reset theContextVarvia token infinally.SentryAsgiMiddlewareas nested only whenrequest_stateis present andnot request_state.response_complete; whenresponse_completeisTrue(pipelined sibling task), enterparent_scopeandparent_isolation_scopebefore forking a freshsentry_sdk.isolation_scope()so pre-request ambient tags still carry over without leaking request-scoped state.tests/integrations/asgi/test_asgi.pyfor pipelinedsend()task spawning and nestedSentryAsgiMiddlewarelayers (294 passedacross ASGI, Starlette, and FastAPI suites).Issues