Skip to content

Commit 796d1d7

Browse files
authored
docs: flask streaming fixed in 4.2.1, aiogram webhook handlers outlive shutdown (#706)
1 parent bad4b78 commit 796d1d7

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

‎docs/integrations/aiogram.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ events, and so does an aiohttp app wired with aiogram's `setup_application()`.
106106
Since modern-di-aiogram 4.3.0, shutdown runs in this order: the plugin waits for the updates still
107107
in flight, then the shutdown handlers run (the dispatcher's, then every sub-router's), and the root
108108
closes last. aiogram can still be handling updates when it emits shutdown, for example when polling
109-
with `handle_as_tasks=True` (the default), so without the wait those handlers could outlive the
110-
container. Shutdown handlers registered after `setup_di` or on sub-routers can resolve
109+
with `handle_as_tasks=True` (the default) or with the webhook handlers in
110+
`aiogram.webhook.aiohttp_server`, inline or in the background, so without the wait those handlers
111+
could outlive the container. Shutdown handlers registered after `setup_di` or on sub-routers can resolve
111112
APP-scoped dependencies, and APP finalizers run last. `setup_di` sets this order on each startup,
112113
so a shutdown handler added during or after startup gets it from the next startup on.
113114

‎docs/integrations/flask.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,12 @@ registered in. It is built on first use, so a `before_request` hook registered b
161161
can use `@inject`, and the close runs after every `teardown_appcontext` handler, including ones
162162
registered after `setup_di`.
163163

164-
!!! warning "`stream_with_context` on Flask 3.1.2 and later"
165-
Flask 3.1.2 and later tear the request context down before the response body is iterated, so
166-
the child is already closed by then. A `stream_with_context` generator that resolves REQUEST
167-
dependencies gets `ContainerClosedError`. modern-di-flask 4.1.0 behaves the same.
164+
Since modern-di-flask 4.2.1, a streamed response keeps the child open until the response closes,
165+
after the body is sent. A `stream_with_context` generator can then resolve REQUEST dependencies on
166+
Flask 3.1.2 and later, and a plain generator response keeps the dependencies the view resolved
167+
live while it streams. A `direct_passthrough` response, such as one from `send_file`, and a request
168+
that raises still close the child at teardown. In 4.1.0 and 4.2.0 a `stream_with_context`
169+
generator got `ContainerClosedError` on Flask 3.1.2 and later.
168170

169171
Because the child closes with `close_sync()`, REQUEST-scoped finalizers must be sync. An async
170172
finalizer raises [`AsyncFinalizerInSyncCloseError`](../troubleshooting/async-finalizer-in-sync-close-error.md),

0 commit comments

Comments
 (0)