Capture and re-establish request-scoped state for deferred stream rendering.
Suspense, Stream, EventStream, and other streaming generators often
run after middlewarefinallyblocks reset the request ContextVars
(request_var, the auth user, CSRF token, and g). The handler resets
these before the streaming/SSE body is drained, so accessing them inside a
deferred render —get_request(), get_user() / current_user(),
get_csrf_token(), or g — would raise LookupErroror return an
AnonymousUserunless we capture them while they are still live and
re-establish them for the drain.
This module is the single capture-then-re-establish path shared by all three
streamed-render types. The carrier (_CapturedRequestContext) is
internal — underscore-prefixed and not exported to the top-level public
surface.
server.streaming_context
| Name | Type | Default | Description |
|---|---|---|---|
type
|
|
— | |
qualified_name
|
|
— | |
element_type
|
|
— | |
description
|
|
— | |
source_file
|
|
— | |
line_number
|
|
— | |
is_autodoc
|
|
— | |
autodoc_element
|
|
— | |
_autodoc_template
|
|
— | |
_autodoc_url_path
|
|
— | |
_autodoc_page_type
|
|
— | |
title
|
|
— | |
doc_content_hash
|
|
— |
Symbols on this page
Snapshot of request-scoped state captured while ContextVars are live.
Carried ontoStreamingResponse/
SSEResponse so the drain (send_streaming_response
forSuspense/Stream, …
Snapshot request/user/CSRF/g/nonce while middleware ContextVars are live.
request_context and csp_nonceare passed in by the caller (the negotiation site already holds the live request…
Stamp live request-scoped snapshots onto a streaming response when unset.
_CapturedRequestContext
class
Snapshot of request-scoped state captured while ContextVars are live.
Carried ontoStreamingResponse/
SSEResponse so the drain (send_streaming_response
forSuspense/Stream, produce_events for EventStream) can
re-establish identical request context. Internal — not public API.
Fields:
auth_user: The authenticated ``User`` (or ``AnonymousUser``), or ``None``
when ``AuthMiddleware`` was not active.
csrf_token: The CSRF token string, or ``None``.
csrf_field_name: The CSRF form field name, or ``None``.
g_snapshot: A shallow copy of ``g``'s store, or ``None`` when ``g`` was
never touched (the zero-``g`` hot path allocates nothing).
request_context: The current `Request`, or
``None``.
csp_nonce: The live CSP nonce, or ``None`` when CSP nonces are disabled.
runtime_context: A `copy_context`() snapshot taken while
middleware and OTel spans are still live, re-attached when the SSE
producer task starts so trace context survives the handler ``finally``.
capture_streaming_render_context
function
def capture_streaming_render_context(*, request_context: Request | None = None, csp_nonce: str | None = None) -> _CapturedRequestContext
Snapshot request/user/CSRF/g/nonce while middleware ContextVars are live.
request_context and csp_nonce are passed in by the caller (the negotiation
site already holds the live request and nonce); the auth user, CSRF token,
andgare read from their ContextVars here.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
request_context
|
Request | None
|
None
|
|
csp_nonce
|
str | None
|
None
|
attach_streaming_render_context
function
def attach_streaming_render_context(response: StreamingResponse) -> StreamingResponse
Stamp live request-scoped snapshots onto a streaming response when unset.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
response
|
StreamingResponse
|
— |
View source · /home/runner/work/chirp/chirp/site/../src/chirp/server/streaming_context.py:1