Skip to content

Dropped exceptions in as_safe_channel #3333

Closed as not planned
Closed as not planned

Description

@A5rocks
async def test8() -> None:
    @trio.as_safe_channel
    async def agen():
        await chan.aclose()
        raise ValueError()
        yield

    async with agen() as chan:
        async for _ in chan:
            pass

This sometimes raises this:

Traceback (most recent call last):
  File "C:\Users\A5rocks\Documents\trio\repro.py", line 124, in <module>
    trio.run(test8)
    ~~~~~~~~^^^^^^^
  File "C:\Users\A5rocks\Documents\trio\src\trio\_core\_run.py", line 2547, in run
    raise runner.main_task_outcome.error
  File "C:\Users\A5rocks\Documents\trio\repro.py", line 121, in test8
    async for _ in chan:
        pass
  File "C:\Users\A5rocks\Documents\trio\src\trio\_abc.py", line 689, in __anext__
    return await self.receive()
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\A5rocks\Documents\trio\src\trio\_channel.py", line 477, in receive
    return await self._recv_chan.receive()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\A5rocks\Documents\trio\src\trio\_channel.py", line 385, in receive
    return await trio.lowlevel.wait_task_rescheduled(abort_fn)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\A5rocks\Documents\trio\src\trio\_core\_traps.py", line 208, in wait_task_rescheduled
    return (await _async_yield(WaitTaskRescheduled(abort_func))).unwrap()
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "C:\Users\A5rocks\Documents\trio\.venv\Lib\site-packages\outcome\_impl.py", line 213, in unwrap
    raise captured_error
trio.ClosedResourceError

Notice no ValueError! Spun off of #3331

Activity

  1. A5rocks commented on Sep 18, 2025

    @A5rocks
    ContributorAuthor

    Hypothesis: this is due to cancellation being suppressed.

  2. A5rocks commented on Sep 18, 2025

    @A5rocks
    ContributorAuthor

    Ok it is. I guess this is user error!! But such a footgun...

    This prints yes, this was cancelled... if and only if we drop the ValueError:

    async def test8() -> None:
        @trio.as_safe_channel
        async def agen():
            try:
                await chan.aclose()
            except trio.Cancelled:
                print("yes, this was cancelled...")
                raise
            raise ValueError()
            yield
    
        async with agen() as chan:
            async for _ in chan:
                pass
  3. Zac-HD commented on Sep 18, 2025

    @Zac-HD
    Member

    😱 it's #455 again, because of course it is 😱

  4. A5rocks commented on Sep 18, 2025

    @A5rocks
    ContributorAuthor

    Actually, I thought so too but... this is just normal cancellation semantics now that I think more about it. I guess cancellation is just hard in general...

  5. jakkdl commented on Sep 19, 2025

    @jakkdl
    Member

    I think I was doing the exact same thing and confusing myself when writing tests for as_safe_channel. Cancellation is definitely tricky!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions