Skip to content

Forward the body exception from BlockingPortalProvider to its wrapped portal context manager - #1366

Merged
agronholm merged 8 commits into
agronholm:masterfrom
feiiiiii5:fix/portal-provider-forward-body-exception
Oct 10, 2026
Merged

agronholm merged 8 commits into
agronholm:masterfrom
feiiiiii5:fix/portal-provider-forward-body-exception

Conversation

@feiiiiii5

Copy link
Copy Markdown
Contributor

NOTE Erasing or replacing the contents of this template will result in your pull
request being summarily closed without consideration!

Changes

Fixes #.

BlockingPortalProvider wraps start_blocking_portal() and delegates both __enter__
and __exit__ to it, but __exit__ invoked the wrapped context manager with a
hard-coded (None, None, None) rather than the exception info it had just been handed:

if portal_cm:
    portal_cm.__exit__(None, None, None)

start_blocking_portal() uses exactly that information to decide what to do with the
tasks still running in the portal:

cancel_remaining_tasks = False
portal = future.result()
try:
    yield portal
except BaseException:
    cancel_remaining_tasks = True
    raise
finally:
    try:
        portal.call(portal.stop, cancel_remaining_tasks)

So an error raised in the with body was never seen by that logic, and the portal's
in-flight tasks were drained to completion instead of cancelled — the one place where
BlockingPortalProvider and start_blocking_portal() diverge. This is the same
divergence the docs invite you to expect, since docs/threads.rst presents the provider
as a cheaper start_blocking_portal().

The fix forwards the three arguments, so except BaseException runs and the remaining
tasks are cancelled. The exception itself is unaffected: the wrapped generator re-raises
the same object, and _GeneratorContextManager.__exit__ returns False in that case, so
nothing is swallowed and no traceback frame is added. Checked for RuntimeError,
KeyboardInterrupt and BaseExceptionGroup — identical type, message and traceback
depth before and after — and the provider is still reusable after an error.

git blame puts the (None, None, None) in 4a23745 ("Added the BlockingPortalProvider
class", #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):

  • You've added tests (in tests/) which would fail without your patch
  • You've updated the documentation (in docs/), in case of behavior changes or new
    features
  • You've added a new changelog entry (in 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:

- Fix big bad boo-boo in task groups
  (`#123 <https://github.com/agronholm/anyio/issues/123>`_; PR by @yourgithubaccount)

If there's no issue linked, just link to your pull request instead by updating the
changelog after you've created the PR.

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
feiiiiii5 force-pushed the fix/portal-provider-forward-body-exception branch 2 times, most recently from c3beb71 to 9965e67 Compare October 8, 2026 06:29
@agronholm
agronholm merged commit 0c0f643 into agronholm:master Oct 10, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants