python / python/cpython

unavoidable spurious "exception in shielded future" logged if exception raised after shield cancelled

オープン
#156,321 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib topic-asyncio type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

test case:

import asyncio

async def fail(t):
    await asyncio.sleep(1)
    raise ValueError("should be caught, not logged")

async def main():
    fut = asyncio.create_task(fail(1))
    try:
        await asyncio.wait_for(asyncio.shield(fut), timeout=0.5)
    except asyncio.TimeoutError:
        print("timeout, as expected")
    
    try:
        await fut
    except Exception as e:
        print("error caught and handled:", e)
    

if __name__ == "__main__":
    asyncio.run(main())

#134331 appears to have introduced a regression where _log_on_exception is added unconditionally and permanently on a future that has been shielded at some point, when the shield future is cancelled, rather than removing the handler when the shield future is done.

In the above code, a long-running future is created. shield It is awaited with a timeout using wait_for(shield(fut)). Since the future is not done, this completes and should leave us in a state just like we had an asyncio.sleep(0.5). However, instead when the future resolves, the error is logged, even though the exception is caught and handled:

> python3 test.py
timeout, as expected
ValueError exception in shielded future
future: <Task finished name='Task-2' coro=<fail() done, defined at /tmp/test.py:3> exception=ValueError('should be caught, not logged')>
Traceback (most recent call last):
  File "/tmp/test.py", line 5, in fail
    raise ValueError("should be caught, not logged")
ValueError: should be caught, not logged
error caught and handled: should be caught, not logged

as far as I can tell, the behavior is more correct if _log_on_exception is removed entirely, but I don't fully track #134331, so it may be solving a problem I don't see.

Maybe I'm missing something? Is there some assumption that the inner future should always be finished before the outer one unless something is wrong? That's what the logging behavior suggests to me.

This is a regression in 3.14. 3.13 and prior appear to have correct logging behavior for failure after shielding.

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs
  • gh-156521

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Lib/asyncio/tasks.py の、issue で参照されている _log_on_exception の処理付近から始め、続いて shield、wait_for、失敗するタスクを使った提供済みの例を Python 3.14 で実行してください。Python 3.13 およびそれ以前のバージョンと動作を比較してください。完了条件は、shield がキャンセルされた後に発生した例外が、元の future がその後 await されて処理されたときにログに記録されないことです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。