classification
Title: "Task exception was never retrieved" reported for a canceled task
Type: behavior Stage: resolved
Components: asyncio Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Miguel Grinberg, ned.deily, yselivanov
Priority: normal Keywords:

Created on 2017-05-30 02:14 by Miguel Grinberg, last changed 2017-06-11 15:42 by ned.deily. This issue is now closed.

Files
File name Uploaded Description Edit
never-retrieved.py Miguel Grinberg, 2017-05-30 02:14
Pull Requests
URL Status Linked Edit
PR 2050 merged yselivanov, 2017-06-09 23:07
PR 2109 merged yselivanov, 2017-06-11 13:38
PR 2110 merged yselivanov, 2017-06-11 13:48
Messages (7)
msg294732 - (view) Author: Miguel Grinberg (Miguel Grinberg) Date: 2017-05-30 02:14
I am seeing a strange issue that occurs when a task that is awaiting an asyncio.wait_for() is cancelled. I created a simple example that I think demonstrates the issue, even though it isn't exactly how it manifests on my application.

When I run the attached script never-retrieved.py I get the following error:

Task exception was never retrieved
future: <Task finished coro=<crash() done, defined at never-retrieved.py:4> exception=ZeroDivisionError('division by zero',)>
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "never-retrieved.py", line 5, in crash
    a = 1/0
ZeroDivisionError: division by zero

You can see in the script that the future was cancelled, but the cancel() call was made after the task finished in a zero division error. I think the cancel() call should update the internal state of the future so that the "exception was never retrieved" error does not appear.

My application has a more complex setup that I have been unable to reproduce with a simple example. I have a task that is waiting on asyncio.wait_for(fut, timeout), with fut subsequently waiting on a websocket server's receive function. When the websocket client closes the connection, a bunch of cancellations happen, but this future inside the wait_for call crashes before wait_for gets to call cancel() on it. Even though I need to investigate this crash, the fact is that wait_for did cancel this future, but because it already ended in an error the "never retried" error is reported anyway.
msg294733 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-05-30 02:15
Yes, this is a known problem to me, thank you for creating the issue. Will work on a fix soon.
msg295708 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-06-11 13:49
New changeset 7ce1c6fb579a01bb184224a10019039fde9c8eaf by Yury Selivanov in branch 'master':
bpo-30508: Don't log exceptions if Task/Future "cancel()" method called (#2050)
https://github.com/python/cpython/commit/7ce1c6fb579a01bb184224a10019039fde9c8eaf
msg295709 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-06-11 14:00
New changeset 176f2ebdad93f20876c08efabd364a0e6c86de14 by Yury Selivanov in branch '3.6':
bpo-30508: Don't log exceptions if Task/Future "cancel()" method was called. (#2109)
https://github.com/python/cpython/commit/176f2ebdad93f20876c08efabd364a0e6c86de14
msg295711 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-06-11 14:11
New changeset d24c8287e226ac9983caf6bb826a7b53142ee31f by Yury Selivanov in branch '3.5':
bpo-30508: Don't log exceptions if Task/Future "cancel()" method was called. (#2110)
https://github.com/python/cpython/commit/d24c8287e226ac9983caf6bb826a7b53142ee31f
msg295712 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-06-11 14:12
Fixed.  Ned, this will be included in 3.6.2, right?
msg295721 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2017-06-11 15:42
Yury, yes (3.6.2 cutoff is about 24 hours from now)
History
Date User Action Args
2017-06-11 15:42:01ned.deilysetmessages: + msg295721
2017-06-11 14:12:51yselivanovsetstatus: open -> closed

versions: + Python 3.7
nosy: + ned.deily

messages: + msg295712
resolution: fixed
stage: resolved
2017-06-11 14:11:49yselivanovsetmessages: + msg295711
2017-06-11 14:00:16yselivanovsetmessages: + msg295709
2017-06-11 13:49:20yselivanovsetmessages: + msg295708
2017-06-11 13:48:42yselivanovsetpull_requests: + pull_request2163
2017-06-11 13:38:11yselivanovsetpull_requests: + pull_request2162
2017-06-09 23:07:57yselivanovsetpull_requests: + pull_request2112
2017-05-30 02:15:28yselivanovsetmessages: + msg294733
2017-05-30 02:14:06Miguel Grinbergcreate