Skip to content
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

Prevent asyncio.wait_for swallowing task cancellation #1698

Merged
merged 3 commits into from
Aug 22, 2024

Conversation

aronhoff
Copy link
Contributor

@aronhoff aronhoff commented Aug 22, 2024

I've been running up against an issue in a project I'm working on, where I cannot cancel an asyncio task while it is waiting on an asyncua call. The CancelledError simply gets swallowed and the task continues as if nothing happened

I traced it down. It turns out, there is a bug in the implementation of asyncio.wait_for, making it swallow the cancellation if the task was cancelled from the outside. See references below.

The bug is only fixed in Python 3.12, existing in one way or another since 3.7. The library wait_for2 provides a fixed implementation for the affected versions.

This PR adds wait_for2 as a dependency replaces calls to asyncio.wait_for with wait_for2.wait_for pre-3.12.

python/cpython#87555
python/cpython#86296
python/cpython#107547

@aronhoff
Copy link
Contributor Author

aronhoff commented Aug 22, 2024

Note that in 3.12, asyncio.wait_for seems to be using an alternative implementation based on asyncio.timeout: python/cpython#96764.

asyncio.timeout was only introduced in 3.11. Backport async-timeout exists for earlier versions, but also seems to have unresolved issues regarding swallowed cancellations:
aio-libs/async-timeout#229
aio-libs/async-timeout#235

In my own project, setting asyncio.wait_for = wait_for2.wait_for at the start of the script, running in 3.11, has fixed the cancellation issues, circumventing asyncua's use of the function.

@oroulet
Copy link
Member

oroulet commented Aug 22, 2024

Did you really manage to reliably stop a task using CancelledError? My experience over several years is that they are always swallowed somehwere, if not a local error in code base then a bug in Python itself as you found out ;-)

@oroulet oroulet merged commit af1b102 into FreeOpcUa:master Aug 22, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants