-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change base class for futures.CancelledError #76709
Comments
I have discoverd one very ugly pattern connected with asyncio. Many times I see code like this: try: Seems, everything is fine, but asyncio.CancelledError unintentionally In order to mitigate thi, we had to write: try: So, what I propose: Basically is to change base class for asyncio.CancelledError Just like 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. |
This is a backwards incompatible change. IMO it's too late to change this. |
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 ? |
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. |
I agree with Yury, this feels too risky to consider. The "except Exception: |
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 ? |
Inheritance from Exception is very annoying, I saw issues with unexpected suppressing CancelledError many times. Even experienced people constantly forget to add a separate But proposed change is backward incompatible, sure. |
Honestly I have no strong opinion. Correct code should not be affected, if somebody want to handle task cancellation explicitly -- he already have 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? |
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. |
Closing this issue as I, personally, don't see this happening and there's no point in keeping it open. |
What a shame, I've seen this error many times as well. Surely making it BaseException will not break that much code?... |
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). |
ping |
Done. 🤞 we don't regret this. |
FYI this has just bitten me after updating my OS to one that ships Python 3.8. It is code that was written with asyncio cancellation in mind and which expected CancelledError to be caught with "except Exception" (the exception block unwound incomplete operations before re-raising the exception). It's obviously too late to do anything about Python 3.8, but I'm mentioning this as a data point in support of having a deprecation period if similar changes are made in future. On the plus side, while fixing up my code and checking all instances of "except Exception" I found some places where this change did fix latent cancellation bugs. So I'm happy with the change, just a little unhappy that it came as a surprise. |
As another datapoint, this also broke some of my code on 3.8 because I was using While I understand the rationale behind this change, it would've been good to include this inheritance detail in the 3.8 release notes. |
Can you send a PR against what’s new 3.8? On Fri, Jul 10, 2020 at 20:14 JustAnotherArchivist <report@bugs.python.org>
-- |
I know this is an old issue, but I just wanted to mention the parallel I note with the same change made for Given the pain of these changes, I wonder if it's worth considering a particularly strenuous review process for exceptions added to any standard library modules to try to make the correct decisions of |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: