You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
unavoidable spurious "exception in shielded future" logged if exception raised after shield cancelled #156321
importasyncioasyncdeffail(t):
awaitasyncio.sleep(1)
raiseValueError("should be caught, not logged")
asyncdefmain():
fut=asyncio.create_task(fail(1))
try:
awaitasyncio.wait_for(asyncio.shield(fut), timeout=0.5)
exceptasyncio.TimeoutError:
print("timeout, as expected")
try:
awaitfutexceptExceptionase:
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.
As well as various links to this issue and PR #156521, this seems to be the root of a report to Django.
asgiref's SyncToAsync has await asyncio.shield(exec_coro), and on cancellation it propagates the cancel into the nested async_to_sync task, then re-awaits exec_coro. So the exception is retrieved, just a tick after _log_on_exception fires. Minimal repro:
Yeah, I think it is generally not true that you can tell if an exception will be retrieved at the time _log_on_exception is called. It could be retrieved at an arbitrary later time, as long as there's still a handle on the task. I think this logging cannot be linked to the error time, it must be linked to the disposal of the reference, which I have no idea how to do.
An alternative that might still fix the issue targeted by #134331 is to retrieve the exception silently (without logging). Upside: it prevents both warnings (spurious "exception in shielded future" and "exception not retrieved"). Downside: it potentially silences a warning (exception not retrieved) that is there for a reason.
I do still think removing the log on cancel altogether is the right thing to do.
Activity
carltongibson commentedon Oct 1, 2026
As well as various links to this issue and PR #156521, this seems to be the root of a report to Django.
asgiref's
SyncToAsynchasawait asyncio.shield(exec_coro), and on cancellation it propagates the cancel into the nestedasync_to_synctask, then re-awaitsexec_coro. So the exception is retrieved, just a tick after_log_on_exceptionfires. Minimal repro:On 3.14.4 this logs
CancelledError exception in shielded future. With the PR #156521 change applied, it's silent.Looks like a candidate to backport to 3.14.
minrk commentedon Oct 1, 2026
Yeah, I think it is generally not true that you can tell if an exception will be retrieved at the time
_log_on_exceptionis called. It could be retrieved at an arbitrary later time, as long as there's still a handle on the task. I think this logging cannot be linked to the error time, it must be linked to the disposal of the reference, which I have no idea how to do.An alternative that might still fix the issue targeted by #134331 is to retrieve the exception silently (without logging). Upside: it prevents both warnings (spurious "exception in shielded future" and "exception not retrieved"). Downside: it potentially silences a warning (exception not retrieved) that is there for a reason.
I do still think removing the log on cancel altogether is the right thing to do.
pythongh-156321: Don't eagerly log exceptions from a cancelled asynci…
kumaraditya303 commentedon Oct 2, 2026
I agree, removing the logging altogether is correct thing to do.
gh-156321: Don't eagerly log exceptions from a cancelled asyncio.shie…
[3.14] gh-156321: Don't eagerly log exceptions from a cancelled async…
[3.15] gh-156321: Don't eagerly log exceptions from a cancelled async…