Repository navigation
Forward the body exception from BlockingPortalProvider to its wrapped portal context manager - #1366
Merged
agronholm merged 8 commits intoOct 10, 2026
Conversation
feiiiiii5
pushed a commit
to feiiiiii5/anyio
that referenced
this pull request
Sep 30, 2026
BlockingPortalProvider wraps start_blocking_portal() and delegates both __enter__ and __exit__ to it, but __exit__ called the wrapped context manager with a hard-coded (None, None, None) instead of the exception info it had just been handed. start_blocking_portal() uses that information to decide whether the tasks still running in the portal should be cancelled, so an error raised in the with body left those tasks to run to completion. Pass the exception info through instead. The exception still propagates unchanged because the wrapped generator re-raises the same object.
The test failed on the Trio backend in CI: the portal was told to cancel before the spawned task had run its first step, so the task never executed and never observed the cancellation. Handshake with a threading.Event so the task is known to be running before the error is raised.
Counting checkpoints was not enough headroom: on a loaded CI runner the 1000 checkpoints could finish before the main thread was scheduled to raise, so the task completed and the test reported a draining portal as if it cancelled. That is what the Trio jobs were hitting, including the case that does not use the provider at all. Have the task wait for a release event instead, with a second task setting it a second later. The task can then only finish by being released, so the draining case is bounded and the cancelling case cannot lose the race.
feiiiiii5
pushed a commit
to feiiiiii5/anyio
that referenced
this pull request
Oct 5, 2026
feiiiiii5
force-pushed
the
fix/portal-provider-forward-body-exception
branch
2 times, most recently
from
October 8, 2026 06:29
c3beb71 to
9965e67
Compare
agronholm
approved these changes
Oct 10, 2026
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.
NOTE Erasing or replacing the contents of this template will result in your pull
request being summarily closed without consideration!
Changes
Fixes #.
BlockingPortalProviderwrapsstart_blocking_portal()and delegates both__enter__and
__exit__to it, but__exit__invoked the wrapped context manager with ahard-coded
(None, None, None)rather than the exception info it had just been handed:start_blocking_portal()uses exactly that information to decide what to do with thetasks still running in the portal:
So an error raised in the
withbody was never seen by that logic, and the portal'sin-flight tasks were drained to completion instead of cancelled — the one place where
BlockingPortalProviderandstart_blocking_portal()diverge. This is the samedivergence the docs invite you to expect, since
docs/threads.rstpresents the provideras a cheaper
start_blocking_portal().The fix forwards the three arguments, so
except BaseExceptionruns and the remainingtasks are cancelled. The exception itself is unaffected: the wrapped generator re-raises
the same object, and
_GeneratorContextManager.__exit__returnsFalsein that case, sonothing is swallowed and no traceback frame is added. Checked for
RuntimeError,KeyboardInterruptandBaseExceptionGroup— identical type, message and tracebackdepth before and after — and the provider is still reusable after an error.
git blameputs the(None, None, None)in4a23745("Added theBlockingPortalProviderclass", #711), so it has been there since the class was introduced and was never covered
by a test.
Checklist
If this is a user-facing code change, like a bugfix or a new feature, please ensure that
you've fulfilled the following conditions (where applicable):
tests/) which would fail without your patchdocs/), in case of behavior changes or newfeatures
docs/versionhistory.rst).Updating the changelog
If there are no entries after the last release, use
**UNRELEASED**as the version.If, say, your patch fixes issue #123, the entry should look like this:
If there's no issue linked, just link to your pull request instead by updating the
changelog after you've created the PR.