`IO.waitAny` from a pool task deadlocks when the thread pool is saturated (`task_manager::wait_any` lacks the worker compensation of `wait_for`)
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 58/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Ambito
- backend, operating-systems
Direzione di ricerca
Read src/runtime/object.cpp, especially task_manager::wait_any at lines 1080–1089 and the worker compensation in wait_for at lines 1056–1078. Reproduce the hang with the provided input.lean and LEAN_NUM_THREADS=1, then compare it with the IO.wait version. Done when IO.waitAny returns the inner task’s result and the outer task finishes under the same conditions.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Prerequisites
- Check that your issue is not already filed:
https://github.com/leanprover/lean4/issues - Reduce the issue to a minimal, self-contained, reproducible test case.
Avoid dependencies to Mathlib or Batteries. - Test your test case against the latest nightly release, for example on
https://live.lean-lang.org/#project=lean-nightly
(You can also use the settings there to switch to “Lean nightly”) - When reporting a perceived soundness bug: Does it affect
lake check --paranoid?
If in doubt, please reach out on [email protected] for responsible disclosure.
(Not a soundness bug.)
Description
wait_for (object.cpp:1056-1078) raises m_max_std_workers and spawns or wakes a worker before a pool task blocks, so a blocked worker cannot starve the queue. wait_any (object.cpp:1080-1089) blocks on m_task_finished_cv without that step. So when every standard worker is inside IO.waitAny on tasks that are still queued, nothing runs them and the program hangs. IO.waitAny is documented to wait until a task finishes and return its result, the same as Task.get.
The program sets the documented LEAN_NUM_THREADS=1 (and LEAN_MAIN_USE_THREAD=0, which does not affect the bug) in an initialize block, then runs IO.asTask for an outer task whose body spawns inner ← IO.asTask (pure (42 : Nat)) and calls IO.waitAny [inner]. The worker that runs the outer task is the only standard worker, so inner is merely queued; the waitAny call never returns: at its entry m_max_std_workers=1, m_std_workers has one entry, m_idle_std_workers=0 and m_queues_size=1, and its two wait_any_check calls correctly return null because inner has no value yet. Nothing can dequeue inner, set its value or notify m_task_finished_cv, so the value that the task body prints after waitAny never appears. Main waits 1 s, reads IO.getTaskState outer = running, prints outerState=running and then calls IO.Process.exit 0 only so that this permanent hang becomes observable (without the forced exit, main and the task-manager destructor would both block on the stuck worker forever). The required behavior is that the same IO.waitAny returns inner's result like Task.get does: wait_for, which implements Task.get/IO.wait in exactly the same situation, raises m_max_std_workers by one and spawns/wakes a worker before sleeping so that the process cannot be deadlocked by threadpool starvation; wait_any omits that compensation.
Documented behavior it contradicts, from the docstring of IO.waitAny (src/Init/System/IO.lean:572):
Waits until any of the tasks in the list has finished, then returns its result.
Context
- Affected function:
task_manager::wait_any(src/runtime/object.cpp), vialean_io_wait_any - Upstream: no existing issue found. Open PR #15743 adds a waiter count to
wait_any, but only for shutdown; the normal-operationm_max_std_workers++/spawn_workerstep stays missing. The cited code is unchanged on nightly-2026-11-26.
Steps to Reproduce
- Save the following as
input.lean(imports only Init/Std):
import Std
initialize setT : Unit ← do
Std.Async.System.setEnvVar "LEAN_MAIN_USE_THREAD" "0"
Std.Async.System.setEnvVar "LEAN_NUM_THREADS" "1"
def main : IO Unit := do
let outer ← IO.asTask do
let inner ← IO.asTask (pure (42 : Nat))
let v ← IO.waitAny [inner]
IO.println s!"inner={v}"
IO.sleep 1000
let state ← IO.getTaskState outer
IO.println s!"outerState={state}"
IO.Process.exit 0
- Compile:
lean -c input.c input.lean && leanc -o prog input.c - Run:
./prog
Expected behavior:
inner=ok: 42
outerState=finished
Actual behavior:
outerState=running
(exit status 0 via the forced IO.Process.exit 0; same in 3 of 3 runs). The same program with IO.waitAny [inner] replaced by IO.wait inner prints the expected inner=ok: 42 / outerState=finished.
Versions
Lean (version 4.38.0-nightly-2026-11-26, arm64-apple-darwin24.6.0, commit e1228b8b45918ece1a00d68e5fc5e6a685ca59ee, Release)
macOS 26.6.2 (arm64)
Additional Information
Root cause analysis. The call comes from a normal (non-sync) task body running on the standard pool (IO.asTask of a body containing IO.waitAny [inner]; caller chain run_task -> lean_io_as_task_fn), with LEAN_NUM_THREADS=1, so the task manager has m_max_std_workers == 1 and m_std_workers.size() == 1; the argument is the well-formed non-empty list [inner], inner being an ordinary task spawned by the same body at default priority. wait_any blocks forever in m_task_finished_cv.wait(lock) while inner is left sitting in m_queues and there is no idle worker, so IO.waitAny never yields the 42 that inner computes. No callee hands it a wrong value: wait_any_check correctly returns nullptr (object.cpp:1081 and 1085) because no listed task has m_value set yet, and enqueue_core does exactly what its code says at m_std_workers.size() == m_max_std_workers (object.cpp:831-834: no spawn, only notify). wait_any is the only waiter in the runtime that sleeps on m_task_finished_cv without first making room when it is itself executing a non-dedicated pool task.
Suggested fix: do the same compensation as wait_for (object.cpp:1062-1076): when called from a pool task (in_pool), m_max_std_workers++ and spawn_worker() or m_queue_cv.notify_one() before waiting, and m_max_std_workers-- afterwards.
Impact
Task-heavy programs that call IO.waitAny from pool tasks can deadlock when the pool is saturated (always with LEAN_NUM_THREADS=1); Lean itself uses IO.waitAny in Elab/Parallel.lean and the server.
Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.
- Lingua principale
- Lean
- Stelle
- 10.3k
- Fork
- 1.1k
- Merge medio
- 16h 45m
- PR unite (30g)
- 361
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di leanprover/lean4
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
leanprover/lean4#16876 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
leanprover/lean4#16842 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
leanprover/lean4#16841 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
leanprover/lean4#16840 ·
I maintainer di solito rispondono entro 1 giorno
-
doc: stray "public" in Bool and HEq documentationForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
leanprover/lean4#16825 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di leanprover/lean4
Issue simili
-
Thai project names become 'project', 'project-2'… (slug keeps ASCII only)Forse già presa @mastel2 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
storytold/craft-anything#2538 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
compiler-explorer/compiler-explorer#9738 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 3 giorni
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
openfoodfacts/openfoodfacts-python#573 ·
I maintainer di solito rispondono entro 2 giorni
-
Rasterio reads over HTTPS do not send the Earthdata token and return 500 "HTTP response code: 404"Apertacredentials rasterio
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
developmentseed/titiler-cmr#359 ·
I maintainer di solito rispondono entro 1 giorno