Skip to content

Commit aec56c1

Browse files
authored
docs: say the starlette and fastapi child close is shielded and bounded (#729)
1 parent 62a9c1f commit aec56c1

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

‎docs/integrations/fastapi.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ To get the child container itself, use `FromDI(modern_di.Container)` or
122122
An HTTP request gets a `Scope.REQUEST` child container and a websocket connection a
123123
`Scope.SESSION` one; see [the scope hierarchy](../providers/scopes.md#what-each-scope-is-for).
124124
`build_di_container` creates the child as a `yield` dependency, so a handler that injects nothing
125-
gets no child, and FastAPI's dependency cleanup closes it.
125+
gets no child, and FastAPI's dependency cleanup closes it. The close is
126+
shielded from cancellation, so a request cancelled by an anyio scope such as `move_on_after` still
127+
runs its finalizers. A close that takes longer than 10 seconds is abandoned, with a warning on the
128+
`modern_di_fastapi.main` logger.
126129

127130
The root container is open from the moment you construct it. `setup_di` wraps the app's lifespan,
128131
and the state yielded by a `lifespan=` you passed to `FastAPI()` passes through. Since

‎docs/integrations/starlette.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ container.open()
169169
An HTTP request gets a `Scope.REQUEST` child container and a websocket connection a
170170
`Scope.SESSION` one; see [the scope hierarchy](../providers/scopes.md#what-each-scope-is-for). The
171171
middleware builds the child before your handler runs and closes it once the app is done with the
172-
connection: after the response is sent for HTTP, and when the socket closes for a websocket.
172+
connection: after the response is sent for HTTP, and when the socket closes for a websocket. The close is
173+
shielded from cancellation, so a request cancelled by an anyio scope such as `move_on_after` still
174+
runs its finalizers. A close that takes longer than 10 seconds is abandoned, with a warning on the
175+
`modern_di_starlette.main` logger.
173176

174177
The root container is open from the moment you construct it. `setup_di` wraps the app's lifespan,
175178
and the state yielded by a `lifespan=` you passed to `Starlette()` passes through. Since

0 commit comments

Comments
 (0)