`IO.waitAny` from a pool task deadlocks when the thread pool is saturated (`task_manager::wait_any` lacks the worker compensation of `wait_for`)

Aperta
#16,822 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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

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

bug
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), via lean_io_wait_any
  • Upstream: no existing issue found. Open PR #15743 adds a waiter count to wait_any, but only for shutdown; the normal-operation m_max_std_workers++ / spawn_worker step stays missing. The cited code is unchanged on nightly-2026-11-26.
Steps to Reproduce
  1. 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
  1. Compile: lean -c input.c input.lean && leanc -o prog input.c
  2. 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

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.