Repository navigation
fix: close the request container even inside a cancelled anyio scope - #89
Merged
Merged
Conversation
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.
Closes #88.
Problem
A concurrent subscriber (
max_workers > 1) runs its handlers in an anyio task group inside the subscriber's consume task. When the subscriber stops,TasksMixin.stopcancels that task, and the task group turns the cancellation into a cancel of its own scope. The DI middleware used to close the request container inside that cancelled scope. anyio cancellation is level-triggered, so everyawaitin the close was cancelled too. The first async finalizer stopped at its firstawait, the rest never ran, and nothing was logged. Any anyio scope a user puts around message handling has the same effect.Sequential subscribers weren't affected: a native
task.cancel()is delivered once, and the close then runs in full.Change
The middleware now closes the request container in
anyio.move_on_after(10, shield=True), the same as modern-di-starlette 4.3.3 and modern-di-fastapi 4.2.2. If the close takes longer than 10 s, the remaining finalizers are abandoned and a warning goes to themodern_di_faststream.mainlogger.TimeoutErrorfromfail_afterwould replace the outer cancellation, and anyio needs that cancellation to unwind its scope.anyio>=4.14.2,<5is now declared, because the module imports it. FastStream itself pins that range.Not covered
A native asyncio
task.cancel()that lands while the close itself is awaiting still interrupts it. An anyio shield can't intercept that.Cost
Entering and leaving the shielded scope costs about 2.1 µs per message on the asyncio backend.
Tests
The test broker calls the handler inline, so each test publishes under
anyio.move_on_afterand cancels the handler while it runs. Both tests failed before the change.Ran on 3.11, 3.14 and 3.14t (25 passed each), and at the declared floors (anyio 4.14.2, faststream 0.7.0).