Issue 32751: wait_for(future, ...) should wait for the future (even if a timeout occurs) (original) (raw)

Created on 2018-02-02 20:01 by njs, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 7216 merged Elvis.Pranskevichus,2018-05-29 19:40
PR 7223 merged miss-islington,2018-05-29 21:32
PR 21895 merged Elvis.Pranskevichus,2020-08-15 22:54
PR 21963 merged miss-islington,2020-08-26 16:42
PR 21967 merged Elvis.Pranskevichus,2020-08-26 18:50
PR 21968 closed Elvis.Pranskevichus,2020-08-26 18:58
Messages (18)
msg311509 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2018-02-02 20:01
Currently, if you use asyncio.wait_for(future, timeout=....) and the timeout expires, then it (a) cancels to the future, and then (b) returns. This is fine if the future is a Future, because Future.cancel is synchronous and completes immediately. But if the future is a Task, then Task.cancel merely requests cancellation, and it will complete later (or not). In particular, this means that wait_for(coro, ...) can return with the coroutine still running, which is surprising. (Originally encountered by Alex Grönholm, who was using code like async with aclosing(agen): await wait_for(agen.asend(...), timeout=...) and then confused about why the call to agen.aclose was raising an error complaining that agen.asend was still running. Currently this requires an async_generator based async generator to trigger; with a native async generator, the problem is masked by bpo-32526.)
msg311515 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-02-02 21:02
Looks like a bug. Andrew, if you have time to look at this, please feel free to go ahead; I'm going to be unavailable till Feb 12 (so I can take a look myself after that).
msg311728 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2018-02-06 15:50
Hmm, I don't see an easy way to fix it. awaiting for cancelled task potentially can wait forever. Adding another timeout looks too confusing. Iterating over a couple of loop steps is not reliable: try/finally block in awaited task can perform async IO with unpredictable completion time.
msg311764 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2018-02-07 06:08
If a task refuses to be cancelled, then is waiting for it forever actually wrong? That's the same thing as happens if I do 'task.cancel(); await task', right? Currently wait_for will abandon such a task, but then it's still left running in the background indefinitely (creating a memory leak or worse).
msg311773 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2018-02-07 08:01
Agree. Should we report about cancelled but still executing tasks? It would be a nice feature. I'm talking not about `wait_for` only but task cancellation in general.
msg311776 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2018-02-07 08:46
How do you tell the difference between a cancelled task that's about to exit, and one that will never exit?
msg311777 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2018-02-07 09:08
Theoretically we can start monitoring cancelled tasks and report about them if the task is still not finished, say, in a minute or two. It is a new feature, sure. I'm fine with waiting for cancelled task in wait_for().
msg318061 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-05-29 18:10
+1 one to fix this in 3.7, Elvis is working on the patch. I don't think we should backport to 3.6 though as it's a behaviour change that people might not expect to get from a bug-fix release.
msg318062 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-05-29 18:22
https://bugs.python.org/issue33638 is an example of a very tricky bug caused by wait_for.
msg318064 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2018-05-29 18:26
Wow, yeah, that is a tricky one. Didn't Ned say, though, that at this point we should be treating 3.7 like an already-released bugfix-only branch?
msg318065 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-05-29 18:31
Well, we'll have another beta (beta 5) and then a release candidate. I think it's enough. I don't feel comfortable with asyncio living with this bug till 3.8.
msg318075 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2018-05-29 19:34
Fair enough. And I can't think of any specific way that fixing this is likely to break anyone, just it's subtle enough that I don't necessarily trust my intuition :-).
msg318076 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-05-29 19:37
> Fair enough. And I can't think of any specific way that fixing this is likely to break anyone, just it's subtle enough that I don't necessarily trust my intuition :-). In case we find out it doesn't work or causes problems during the beta/rc period, we will simply pull it out.
msg318095 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-05-29 21:31
New changeset e2b340ab4196e1beb902327f503574b5d7369185 by Yury Selivanov (Elvis Pranskevichus) in branch 'master': bpo-32751: Wait for task cancellation in asyncio.wait_for() (GH-7216) https://github.com/python/cpython/commit/e2b340ab4196e1beb902327f503574b5d7369185
msg318130 - (view) Author: miss-islington (miss-islington) Date: 2018-05-29 22:37
New changeset d8948c5e09c4a2a818f6f6cfaf8064f2c2138fa5 by Miss Islington (bot) in branch '3.7': bpo-32751: Wait for task cancellation in asyncio.wait_for() (GH-7216) https://github.com/python/cpython/commit/d8948c5e09c4a2a818f6f6cfaf8064f2c2138fa5
msg375938 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2020-08-26 16:42
New changeset c517fc712105c8e5930cb42baaebdbe37fc3e15f by Elvis Pranskevichus in branch 'master': bpo-32751: Wait for task cancel in asyncio.wait_for() when timeout <= 0 (#21895) https://github.com/python/cpython/commit/c517fc712105c8e5930cb42baaebdbe37fc3e15f
msg375941 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2020-08-26 17:15
New changeset 1036ccb55de4abc70837cb46a72ddbb370b8fc94 by Miss Islington (bot) in branch '3.9': bpo-32751: Wait for task cancel in asyncio.wait_for() when timeout <= 0 (GH-21895) (GH-21963) https://github.com/python/cpython/commit/1036ccb55de4abc70837cb46a72ddbb370b8fc94
msg375960 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2020-08-26 20:59
New changeset 57b698886b47bb81c782c2ba80a8a72fe66c7aad by Elvis Pranskevichus in branch '3.8': [3.8] bpo-32751: Wait for task cancel in asyncio.wait_for() when timeout <= 0 (GH-21895) (#21967) https://github.com/python/cpython/commit/57b698886b47bb81c782c2ba80a8a72fe66c7aad
History
Date User Action Args
2022-04-11 14:58:57 admin set github: 76932
2020-08-26 20:59:39 yselivanov set messages: +
2020-08-26 18:58:56 Elvis.Pranskevichus set pull_requests: + <pull%5Frequest21077>
2020-08-26 18:50:14 Elvis.Pranskevichus set pull_requests: + <pull%5Frequest21076>
2020-08-26 17:15:08 lukasz.langa set nosy: + lukasz.langamessages: +
2020-08-26 16:42:56 miss-islington set pull_requests: + <pull%5Frequest21073>
2020-08-26 16:42:52 yselivanov set messages: +
2020-08-15 22:54:11 Elvis.Pranskevichus set nosy: + Elvis.Pranskevichuspull_requests: + <pull%5Frequest21014>
2018-05-30 01:01:02 yselivanov set status: open -> closedtype: behaviorresolution: fixedstage: patch review -> resolved
2018-05-29 22:37:09 miss-islington set nosy: + miss-islingtonmessages: +
2018-05-29 21:32:27 miss-islington set pull_requests: + <pull%5Frequest6854>
2018-05-29 21:31:05 yselivanov set messages: +
2018-05-29 19:40:57 Elvis.Pranskevichus set keywords: + patchstage: patch reviewpull_requests: + <pull%5Frequest6848>
2018-05-29 19:37:36 yselivanov set messages: +
2018-05-29 19:34:54 njs set messages: +
2018-05-29 18:31:41 yselivanov set messages: +
2018-05-29 18:26:02 njs set messages: +
2018-05-29 18:24:04 yselivanov link issue33638 superseder
2018-05-29 18:22:29 yselivanov set messages: +
2018-05-29 18:10:31 yselivanov set messages: + versions: - Python 3.5, Python 3.6
2018-02-11 02:45:45 xgdomingo set nosy: + xgdomingo
2018-02-07 09:08:31 asvetlov set messages: +
2018-02-07 08:46:38 njs set messages: +
2018-02-07 08:01:20 asvetlov set messages: +
2018-02-07 06:08:36 njs set messages: +
2018-02-06 15:50:36 asvetlov set messages: +
2018-02-02 21:02:20 yselivanov set messages: +
2018-02-02 20:01:15 njs create