{{ message }}
GH-96764: rewrite asyncio.wait_for to use asyncio.timeout - #98518
Merged
Merged
Conversation
kumaraditya303
force-pushed
the
waitfor
branch
2 times, most recently
from
October 21, 2022 13:09
45d0725 to
6c944d9
Compare
Contributor
Author
gvanrossum
reviewed
Oct 21, 2022
gvanrossum
left a comment
Member
There was a problem hiding this comment.
You got a bit farther than I did when I tried this, but I'm still concerned why you had to modify several tests. (Also, there's one test that fails in CI, but doesn't appear to fail when I run it locally.)
gvanrossum
reviewed
Oct 21, 2022
gvanrossum
reviewed
Oct 22, 2022
gvanrossum
reviewed
Oct 22, 2022
gvanrossum
approved these changes
Oct 24, 2022
kumaraditya303
marked this pull request as ready for review
October 24, 2022 06:03
Member
|
I had hoped to first merge one of the alternatives that can be backported to 3.10, so we can declare this fixed in 3.10. Then we would merge this one on top of that, but only in 3.11 and main. But we could also just do a custom fix for 3.10 based on one of @twisteroidambassador's PRs. (I wish you had linked this PR to the same issue rather than creating a new issue, since it's all related.) |
Member
akulakov
reviewed
Nov 3, 2022
a new task so no sleep is required
bdraco
added a commit
to bdraco/python-kasa
that referenced
this pull request
Jul 21, 2023
Fallback to using async_timeout on older python asyncio.wait_for has some underlying problems that are only fixed in cpython 3.12. See python/cpython#98518
rytilahti
pushed a commit
to python-kasa/python-kasa
that referenced
this pull request
Jul 21, 2023
asyncio.wait_for has some underlying problems that are only fixed in cpython 3.12. Use async_timeout instead until the minimum supported version is 3.11+ and it can be replaced with asyncio.timeout See python/cpython#98518
bdraco
added a commit
to bdraco/ha-HAP-python
that referenced
this pull request
Jul 23, 2023
async_timeout does not suffer from the same race problems as asyncio.wait_for see python/cpython#98518 for more details
SomberNight
added a commit
to SomberNight/electrum
that referenced
this pull request
Aug 4, 2023
wasted some time because asyncio.wait_for() was suppressing cancellations. [0][1][2] deja vu... [3] Looks like this is finally getting fixed in cpython 3.12 [4] So far away... In attempt to avoid encountering this again, let's try using asyncio.timeout in 3.11, which is how upstream reimplemented wait_for in 3.12 [4], and aiorpcx.timeout_after in 3.8-3.10. [0] python/cpython#86296 [1] https://bugs.python.org/issue42130 [2] https://bugs.python.org/issue45098 [3] kyuupichan/aiorpcX#44 [4] python/cpython#98518
SomberNight
added a commit
to spesmilo/electrum
that referenced
this pull request
Aug 4, 2023
wasted some time because asyncio.wait_for() was suppressing cancellations. [0][1][2] deja vu... [3] Looks like this is finally getting fixed in cpython 3.12 [4] So far away... In attempt to avoid encountering this again, let's try using asyncio.timeout in 3.11, which is how upstream reimplemented wait_for in 3.12 [4], and aiorpcx.timeout_after in 3.8-3.10. [0] python/cpython#86296 [1] https://bugs.python.org/issue42130 [2] https://bugs.python.org/issue45098 [3] kyuupichan/aiorpcX#44 [4] python/cpython#98518
bdraco
added a commit
to bdraco/python-androidtv
that referenced
this pull request
Aug 31, 2023
`asyncio.wait_for` creates another tasks which leads to some race conditions in cancelation and a performance hit cpython 3.12 will change the underlying implementation of `asyncio.wait_for` to use `asyncio.wait` but that is still a long way off for many people: python/cpython#98518
JeffLIrion
pushed a commit
to JeffLIrion/python-androidtv
that referenced
this pull request
Sep 1, 2023
* Switch usage of asyncio.wait_for to async_timeout `asyncio.wait_for` creates another tasks which leads to some race conditions in cancelation and a performance hit cpython 3.12 will change the underlying implementation of `asyncio.wait_for` to use `asyncio.wait` but that is still a long way off for many people: python/cpython#98518 * adjust ci
32 tasks
20 tasks
12 tasks
aconchillo
added a commit
to pipecat-ai/pipecat
that referenced
this pull request
Aug 20, 2025
This is an implementation of Python's `asyncio.timeout()` which was introduced
in 3.11. We strongly encourage the use of the following approach instead of
`asyncio.wait_for()`.
async with timeout(delay):
# Your async code
In Python 3.12, `asyncio.wait_for()` is implemented in terms of
`asyncio.timeout()` which fixed a bunch of issues. However, this was never
backported (because of the lack of `async.timeout()`) and there are still many
remainig issues, specially in Python 3.10, in `async.wait_for()`.
See python/cpython#98518
aconchillo
added a commit
to pipecat-ai/pipecat
that referenced
this pull request
Aug 20, 2025
This is an implementation of Python's `asyncio.timeout()` which was introduced
in 3.11. We strongly encourage the use of the following approach instead of
`asyncio.wait_for()`.
async with timeout(delay):
# Your async code
In Python 3.12, `asyncio.wait_for()` is implemented in terms of
`asyncio.timeout()` which fixed a bunch of issues. However, this was never
backported (because of the lack of `async.timeout()`) and there are still many
remainig issues, specially in Python 3.10, in `async.wait_for()`.
See python/cpython#98518
aconchillo
added a commit
to pipecat-ai/pipecat
that referenced
this pull request
Aug 20, 2025
This patch implements `asyncio.timeout()` which was introduced in 3.11, as well +as `asyncio.wait_for()` on top of it. In Python 3.12, `asyncio.wait_for()` is implemented in terms of `asyncio.timeout()` which fixed a bunch of issues. However, this was never backported (because of the lack of `async.timeout()`) and there are still many remainig issues, specially in Python 3.10, in `async.wait_for()`. See python/cpython#98518
aconchillo
added a commit
to pipecat-ai/pipecat
that referenced
this pull request
Aug 20, 2025
This patch implements `asyncio.timeout()` which was introduced in 3.11, as well +as `asyncio.wait_for()` on top of it. In Python 3.12, `asyncio.wait_for()` is implemented in terms of `asyncio.timeout()` which fixed a bunch of issues. However, this was never backported (because of the lack of `async.timeout()`) and there are still many remainig issues, specially in Python 3.10, in `async.wait_for()`. See python/cpython#98518
aconchillo
added a commit
to pipecat-ai/pipecat
that referenced
this pull request
Aug 20, 2025
This patch implements a custom `asyncio.wait_for()` on top of an also custom `asyncio.timeout()`. `asyncio.timeout()` which was introduced in 3.11. In Python 3.12, `asyncio.wait_for()` is implemented in terms of `asyncio.timeout()` which fixed a bunch of issues. However, this was never backported (because of the lack of `async.timeout()`) and there are still many remainig issues, specially in Python 3.10, in `async.wait_for()`. See python/cpython#98518
aconchillo
added a commit
to pipecat-ai/pipecat
that referenced
this pull request
Aug 20, 2025
This patch implements a custom `asyncio.wait_for()` on top of an also custom `asyncio.timeout()`. `asyncio.timeout()` which was introduced in 3.11. In Python 3.12, `asyncio.wait_for()` is implemented in terms of `asyncio.timeout()` which fixed a bunch of issues. However, this was never backported (because of the lack of `async.timeout()`) and there are still many remainig issues, specially in Python 3.10, in `async.wait_for()`. See python/cpython#98518
aconchillo
added a commit
to pipecat-ai/pipecat
that referenced
this pull request
Aug 20, 2025
This patch uses `wait_for2` package to implement `asyncio.wait_for()` for Python < 3.12. In Python 3.12, `asyncio.wait_for()` is implemented in terms of `asyncio.timeout()` which fixed a bunch of issues. However, this was never backported (because of the lack of `async.timeout()`) and there are still many remainig issues, specially in Python 3.10, in `async.wait_for()`. See python/cpython#98518
aconchillo
added a commit
to pipecat-ai/pipecat
that referenced
this pull request
Aug 20, 2025
This patch uses `wait_for2` package to implement `asyncio.wait_for()` for Python < 3.12. In Python 3.12, `asyncio.wait_for()` is implemented in terms of `asyncio.timeout()` which fixed a bunch of issues. However, this was never backported (because of the lack of `async.timeout()`) and there are still many remainig issues, specially in Python 3.10, in `async.wait_for()`. See python/cpython#98518
gtopper
pushed a commit
to gtopper/storey
that referenced
this pull request
Apr 19, 2026
`SimpleAsyncQueue.put()` accepted a `capacity` parameter but never enforced it — items were appended unconditionally, making the queue effectively unbounded and defeating backpressure. Added `_not_full_futures` to block `put()` when the deque reaches capacity, mirroring the existing `_not_empty_futures` pattern in `get()`. Updated docstring to document why this class exists (Python < 3.12 `asyncio.wait_for` item-swallowing bug, python/cpython#98518). Updated `test_queue.py` with assertions that `put()` blocks at capacity and resumes after `get()`.
gtopper
added a commit
to mlrun/storey
that referenced
this pull request
Apr 20, 2026
* Fix `SimpleAsyncQueue` missing capacity enforcement `SimpleAsyncQueue.put()` accepted a `capacity` parameter but never enforced it — items were appended unconditionally, making the queue effectively unbounded and defeating backpressure. Added `_not_full_futures` to block `put()` when the deque reaches capacity, mirroring the existing `_not_empty_futures` pattern in `get()`. Updated docstring to document why this class exists (Python < 3.12 `asyncio.wait_for` item-swallowing bug, python/cpython#98518). Updated `test_queue.py` with assertions that `put()` blocks at capacity and resumes after `get()`. * Use self._loop consistently, assert on capacity invariant * Rename variables for consistency with `asyncio.Queue` * Fix putter starvation in SimpleAsyncQueue A new `put()` could steal a slot freed by `get()` before a waiting putter had a chance to resume, starving earlier waiters indefinitely. Replaced `len(deque)`-based capacity check with a `_size` counter that tracks claimed capacity (items in deque + slots reserved for woken putters). `get()` only decrements `_size` when there are no waiting putters; otherwise it transfers the slot directly to the next waiter. This guarantees FIFO ordering and prevents starvation. * Assert size. Add clarifying comment. --------- Co-authored-by: Gal Topper <galt@iguazio.com>
12 tasks
Levidev15
pushed a commit
to Levidev15/Botelier2.0
that referenced
this pull request
Jun 5, 2026
This patch uses `wait_for2` package to implement `asyncio.wait_for()` for Python < 3.12. In Python 3.12, `asyncio.wait_for()` is implemented in terms of `asyncio.timeout()` which fixed a bunch of issues. However, this was never backported (because of the lack of `async.timeout()`) and there are still many remainig issues, specially in Python 3.10, in `async.wait_for()`. See python/cpython#98518
TheButterZone
pushed a commit
to TheButterZone/electrum
that referenced
this pull request
Aug 31, 2026
wasted some time because asyncio.wait_for() was suppressing cancellations. [0][1][2] deja vu... [3] Looks like this is finally getting fixed in cpython 3.12 [4] So far away... In attempt to avoid encountering this again, let's try using asyncio.timeout in 3.11, which is how upstream reimplemented wait_for in 3.12 [4], and aiorpcx.timeout_after in 3.8-3.10. [0] python/cpython#86296 [1] https://bugs.python.org/issue42130 [2] https://bugs.python.org/issue45098 [3] kyuupichan/aiorpcX#44 [4] python/cpython#98518
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

This PR changes
asyncio.wait_forto useasyncio.timeoutas its underlying implementation. It simplifies the code and makes it easy to understand the cancellation semantics as bothasyncio.timeoutandasyncio.wait_forbehaves similarly.Fixes #86296
Fixes #81839
Fixes #96764
asyncio.wait_forusingasyncio.timeout#96764