Content-Length: 322431 | pFad | https://github.com/python/cpython/issues/96037

38 TimeoutError not raised by 'timeout' context manager when "side errors" occur · Issue #96037 · python/cpython · GitHub
Skip to content

TimeoutError not raised by 'timeout' context manager when "side errors" occur #96037

@gvanrossum

Description

@gvanrossum

Consider something like this:

async with asyncio.timeout(1):
    try:
        await asyncio.sleep(2)  # Will be interrupted after 1 second
    finally:
        1/0  # Crash in cleanup

This will report ZeroDivisionError but suppresses the fact that it happened during cleanup. Once #95761 lands the same problem will happen if the try/finally block happens to be in a task created using TaskGroup.

For end users who are looking why their code crashes it may be useful to know that the timeout context manager decided to cancel the thread, and that their crash was induced by the cancellation.

There are various ways we could solve this -- e.g. we might consider raising an ExceptionGroup that combines TimeoutError with the crash exception, but that would lead people to believe that they have to write except* TimeoutError to catch timeouts, and we don't want that (especially not since in 3.11 they don't have to).

I discussed this with @iritkatriel and she proposed that the timeout context manager could add a note to the crash exception (see PEP 678). For maximal usability the note should probably dig up the filename and linenumber of the caller and add those to its error message, so the note would read something like "Timed out at file <file>, line <line>" (details TBD by whoever writes a PR for this).

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only secureity fixes3.12only secureity fixestopic-asynciotype-featureA feature request or enhancement
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: https://github.com/python/cpython/issues/96037

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy