Title: Change base class for futures.CancelledError
Type: enhancement Stage: resolved
Components: asyncio, Library (Lib) Versions: Python 3.8
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Dima.Tisnek, achimnol, asvetlov, chris.jerdonek, gustavo, gvanrossum, socketpair, yselivanov
Priority: normal Keywords: patch

Created on 2018-01-10 16:31 by socketpair, last changed 2019-05-27 12:45 by yselivanov. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 13528 merged yselivanov, 2019-05-23 21:38
Messages (15)
msg309772 - (view) Author: Марк Коренберг (socketpair) * Date: 2018-01-10 16:31
I have discoverd one very ugly pattern connected with asyncio. Many times I see code like this:

    await something()
except Exception:
    log.error('Opertaion failed -- will retry later.')

Seems, everything is fine, but asyncio.CancelledError unintentionally
also suppressed in that code. So, sometimes coroutines are not cancellable.

In order to mitigate thi, we had to write:

    await something()
except CancelledError:
except Exception:
    log.error('Opertaion failed. will retry later.')

So, what I propose: Basically is to change base class for asyncio.CancelledError
from Exception (yes, I know concurrent.futures and it's `Error` class) to BaseException.

Just like `SystemExit` and other SPECIAL exceptions.

Yes, I know that it would be incompatible change. But I suspect that impact will be minimal. Documentation for concurrent.futures and asyncio does not say that this exception is derived from Exception.
msg309775 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-01-10 18:27
This is a backwards incompatible change.  IMO it's too late to change this.
msg309776 - (view) Author: Марк Коренберг (socketpair) * Date: 2018-01-10 18:35
Will you accept PR if I fix that ?

I think we may merge that in Python 3.8

Who can also judge us? @asvetlov, what do you think about my idea ?
msg309777 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-01-10 18:43
While I understand the reasons for the proposed change, I'd still be -1 for it. Solely on the basis of "we don't know how much this change will break, but it will surely break something in very subtle ways".

Another problem is that asyncio currently doesn't handle BaseExceptions that well.

I'll put Guido in the nosy list, maybe he'll have a different opinion on this.
msg309779 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2018-01-10 19:15
I agree with Yury, this feels too risky to consider. The "except Exception:
<log>" code is at fault.
msg309796 - (view) Author: Марк Коренберг (socketpair) * Date: 2018-01-11 08:52

More real code:

async def xxxx():
    while True:
            result = await download()
        except Exception as e:
            log.warning('Fail..%r', e)
        await asyncio.sleep()

Why sucha a code is fault ?
msg309799 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2018-01-11 09:43
Inheritance from Exception is very annoying, I saw issues with unexpected suppressing CancelledError many times.

Even experienced people constantly forget to add a separate `except asyncio.CancelledError` clause everywhere.

But proposed change is backward incompatible, sure.
msg309800 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2018-01-11 09:48
Honestly I have no strong opinion.

Correct code should not be affected, if somebody want to handle task cancellation explicitly -- he already have `except CancelledError` in his code.

What are use cases for intentional catching Exception for task cancellation prevention?

Could we add a warning for this case without base exception class change? I don't see how to do it but maybe somebody has an idea?
msg309894 - (view) Author: Joongi Kim (achimnol) * Date: 2018-01-13 18:03
I strongly agree to have discretion of CancelledError and other general exceptions, though I don't have concrete ideas on good unobtrusive ways to achieve this.

If I write my codes carefully I could control most of cancellation explicitly, but it is still hard to control it in 3rd-party libraries that I depend on. Often they just raise random errors, or CancelledError is swallowed.

Also it would be nice to have some documentation and examples on how to write "cancellation-friendly" coroutine codes.
msg317705 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-05-25 19:40
Closing this issue as I, personally, don't see this happening and there's no point in keeping it open.
msg326274 - (view) Author: Gustavo J. A. M. Carneiro (gustavo) * Date: 2018-09-24 17:42
What a shame, I've seen this error many times as well.

Surely making it BaseException will not break that much code?...
msg326275 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-09-24 17:47
> Closing this issue as I, personally, don't see this happening and there's no point in keeping it open.

Actually, Andrew and I changed our opinion on this, so I'm re-opening the issue.

After visiting three conferences this summer and talking to asyncio users, it seems that this is a very serious pitfall.  At least 8 different people shared stories about really hard to debug problems caused by "except Exception" code blocking cancellation.

I now think we should fix this and make CancelledError a BaseException.  Doing that isn't as straightforward as it seems as we have to first fix how asyncio handles BaseExceptions (my next ToDo).
msg337839 - (view) Author: Dima Tisnek (Dima.Tisnek) * Date: 2019-03-13 09:25
msg343618 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2019-05-27 12:45
New changeset 431b540bf79f0982559b1b0e420b1b085f667bb7 by Yury Selivanov in branch 'master':
bpo-32528: Make asyncio.CancelledError a BaseException. (GH-13528)
msg343619 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2019-05-27 12:45
Done. 🤞 we don't regret this.
Date User Action Args
2019-05-27 12:45:50yselivanovsetstatus: open -> closed
resolution: fixed
messages: + msg343619

stage: patch review -> resolved
2019-05-27 12:45:15yselivanovsetmessages: + msg343618
2019-05-23 21:38:08yselivanovsetkeywords: + patch
stage: patch review
pull_requests: + pull_request13442
2019-03-13 09:25:39Dima.Tisneksetnosy: + Dima.Tisnek
messages: + msg337839
2019-02-20 05:15:27chris.jerdoneksetnosy: + chris.jerdonek
2018-09-24 17:47:33yselivanovsetstatus: closed -> open
versions: - Python 3.7
messages: + msg326275

resolution: rejected -> (no value)
stage: resolved -> (no value)
2018-09-24 17:42:14gustavosetnosy: + gustavo
messages: + msg326274
2018-05-25 19:40:43yselivanovsetstatus: open -> closed
resolution: rejected
messages: + msg317705

stage: resolved
2018-01-13 18:03:34achimnolsetmessages: + msg309894
2018-01-12 16:35:46achimnolsetnosy: + achimnol
2018-01-11 09:48:19asvetlovsetmessages: + msg309800
2018-01-11 09:43:18asvetlovsetmessages: + msg309799
2018-01-11 08:52:28socketpairsetmessages: + msg309796
2018-01-10 19:15:33gvanrossumsetmessages: + msg309779
2018-01-10 18:43:49yselivanovsetnosy: + gvanrossum
messages: + msg309777
2018-01-10 18:35:07socketpairsetmessages: + msg309776
2018-01-10 18:27:31yselivanovsetmessages: + msg309775
2018-01-10 16:37:14socketpairsetcomponents: + Library (Lib)
2018-01-10 16:31:24socketpairsettype: enhancement
2018-01-10 16:31:15socketpaircreate