classification
Title: Change base class for futures.CancelledError
Type: enhancement Stage:
Components: asyncio, Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: achimnol, asvetlov, gustavo, gvanrossum, socketpair, yselivanov
Priority: normal Keywords:

Created on 2018-01-10 16:31 by socketpair, last changed 2018-09-24 17:47 by yselivanov.

Messages (12)
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:


try:
    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:



try:
    await something()
except CancelledError:
    raise
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
@gvanrossum

More real code:

async def xxxx():
    while True:
        try:
            result = await download()
            handle_result(result)
        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).
History
Date User Action Args
2018-09-24 17:47:33yselivanovsetstatus: closed -> open
versions: - Python 3.7
messages: + msg326275

resolution: rejected ->
stage: resolved ->
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