Skip to content

Cancelling a Future from BlockingPortal.start_task(_soon)? doesn't cancel the async function if portal.stop() has been called #1013

@gschaffner

Description

@gschaffner

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

AnyIO version

master (14356be)

Python version

CPython 3.13.7

What happened?

Cancelling a Future from BlockingPortal.start_task/BlockingPortal.start_task_soon does nothing if the portal has been requested to stop (portal.stop).

How can we reproduce the bug?

This test is nearly identical to test_start_task_soon_cancel_immediately, but with portal.call(portal.stop) added:

    def test_start_task_soon_cancel_after_request_stop(
        self, anyio_backend_name: str, anyio_backend_options: dict[str, Any]
    ) -> None:
        cancelled = False
        done_event = threading.Event()

        async def event_waiter() -> None:
            nonlocal cancelled
            try:
                await sleep(3)
            except get_cancelled_exc_class():
                cancelled = True
            finally:
                done_event.set()

        with start_blocking_portal(anyio_backend_name, anyio_backend_options) as portal:
            future = portal.start_task_soon(event_waiter)
            portal.call(portal.stop)
            future.cancel()
            done_event.wait(10)

        assert cancelled

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions