Skip to content

shielded task exception never retrieved when outer task cancelled #86802

Closed

Description

@natano
mannequin
BPO 42636
Nosy @asvetlov, @1st1, @natano
Files
  • trigger-warning.py: Short script for showing the issue
  • regression-test.patch: Regression test
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2020-12-14.10:36:21.291>
    labels = ['3.7', '3.8', '3.9', '3.10', 'expert-asyncio']
    title = 'shielded task exception never retrieved when outer task cancelled'
    updated_at = <Date 2020-12-15.09:44:41.561>
    user = 'https://github.com/natano'

    bugs.python.org fields:

    activity = <Date 2020-12-15.09:44:41.561>
    actor = 'natano'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['asyncio']
    creation = <Date 2020-12-14.10:36:21.291>
    creator = 'natano'
    dependencies = []
    files = ['49679', '49680']
    hgrepos = []
    issue_num = 42636
    keywords = ['patch']
    message_count = 4.0
    messages = ['382975', '382976', '382979', '383039']
    nosy_count = 3.0
    nosy_names = ['asvetlov', 'yselivanov', 'natano']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue42636'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']

    Linked PRs

    Activity

    1. natano commented on Dec 14, 2020

      natanomannequin
      MannequinAuthor

      A task created with asyncio.shield() never retrieves the task exception, which results in a log message being generated. See the attached script for a minimal example. Output looks something like this:

      <snip>
      Task exception was never retrieved
      future: <Task finished name='Task-2' coro=<some_coroutine() done, defined at /Users/natano/python-bug/trigger-warning.py:4> exception=Exception('foo')>
      Traceback (most recent call last):
        File "/Users/natano/python-bug/trigger-warning.py", line 6, in some_coroutine
          raise Exception('foo')
      Exception: foo
      </snip>

      I believe this behaviour is not intended and is an unintended side-effect of commit b35acc5 (b35acc5). The _inner_done_callback has has code to retrieve the exception when the outer task was cancelled, but since that commit the _inner_done_callback is not called anymore when the outer task was cancelled.

      (Tested with 3.8, 3.9 and 3.10. I don't have a python 3.7 version available for testing, but I think this issue also affects 3.7.)

    2. natano commented on Dec 14, 2020

      natanomannequin
      MannequinAuthor

      I'm not sure how to fix this, but here is a regression test for the issue. (failing on the current master branch)

    3. asvetlov commented on Dec 14, 2020

      @asvetlov
      Contributor

      Thanks for the report.

      I didn't test but looks like outer.exception() call can suppress the message about not retrieved exception.

    4. natano commented on Dec 15, 2020

      natanomannequin
      MannequinAuthor

      How would calling outer.exception() suppress the message? This happens when the outer task is cancelled while the inner task is still running. In that case the exception is never set on outer.

    5. transferred this issue fromon Apr 10, 2022
    6. 5 remaining items

    7. added a commit that references this issue on May 20, 2025
    8. added a commit that references this issue on May 20, 2025
    9. added a commit that references this issue on May 20, 2025
    10. added
      type-bugAn unexpected behavior, bug, or error
      stdlibStandard Library Python modules in the Lib/ directory
      on May 20, 2025
    11. added a commit that references this issue on May 20, 2025
    12. moved this from Todo to Done in asyncioon May 20, 2025
    13. added 3 commits that reference this issue on May 20, 2025
    14. added a commit that references this issue on May 26, 2025
    15. added a commit that references this issue on Jul 12, 2025
    16. added a commit that references this issue on Aug 4, 2025
    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

      stdlibStandard Library Python modules in the Lib/ directorytopic-asynciotype-bugAn unexpected behavior, bug, or error

      Projects

      • Status
        Done

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions