task_manager::resolve_core calls m_task_finished_cv.notify_all() on every completion, causing thundering-herd wakeups
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- cpp
- Domain
- backend, operating-systems, performance
Research direction
Start with src/runtime/object.cpp, especially task_manager::resolve_core and the wait_for/wait_any paths that use m_task_finished_cv; the issue includes a Lean benchmark reproducer. Read how task completion and waiter synchronization interact, then verify that unrelated completions no longer wake task-specific waiters while IO.waitAny still works. The scope is concurrency-sensitive and spans runtime behavior.
Written by the indexing model from the issue text.
Description
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
master(4.38.0-pre, commit3158840ce555).
Description
In src/runtime/object.cpp, task_manager uses a single process-wide m_task_finished_cv for all threads blocked in wait_for (Task.get / IO.wait / Async.block) and wait_any (IO.waitAny). Every task or promise completion in resolve_core unconditionally calls m_task_finished_cv.notify_all(), waking every waiting thread across the entire process to contend on m_mutex, check t->m_value != nullptr, and go back to sleep (O(waiters × completions)).
Context
We hit this bottleneck in a multithreaded Std.Net / Std.Async server workload: replacing Async.block waits with per-waiter parking cut Std.Net TCP echo latency from 97 µs → 75 µs (-23%) and context switches from 2.9 → 1.7 (-41%) at 16 concurrent connections.
Steps to Reproduce
Compile and run the following benchmark (lean --c=bench.c bench.lean && leanc -O3 -DNDEBUG bench.c -leanshared -lleanshared -o bench && ./bench), which parks 32 dedicated threads in IO.wait on distinct unresolved promises while resolving 10,000 unrelated promises:
def main : IO Unit := do
let numSleepers := 32
let numCompletions := 10000
let mut sleepers : Array (IO.Promise Unit × Task (Except IO.Error Unit)) := #[]
let ready ← IO.mkRef 0
for _ in [:numSleepers] do
let p ← IO.Promise.new
let t ← IO.asTask (prio := .dedicated) do
ready.modify (· + 1)
let _ ← IO.wait p.result?
sleepers := sleepers.push (p, t)
while (← ready.get) < numSleepers do
IO.sleep 1
IO.sleep 20
let t0 ← IO.monoNanosNow
for i in [:numCompletions] do
let p ← IO.Promise.new
p.resolve i
let t1 ← IO.monoNanosNow
for (p, t) in sleepers do
p.resolve ()
let _ ← IO.wait t
IO.println s!"elapsed: {(t1 - t0) / 1000} us"
Expected behavior: Unrelated promise resolutions do not wake threads waiting on different tasks (O(1) per completion).
Actual behavior: Every resolution wakes all 32 sleeping threads (~68 ms for 10,000 empty resolutions, 0.51 s kernel sys time, 21,069 involuntary context switches on macOS arm64).
Versions
4.38.0-pre(mastercommit3158840ce555275ad679a47fd224d887b45d3d88, macOS arm64)
Additional Information
A zero-per-wait-allocation fix is straightforward:
- Add
struct lean_task_waiter * m_waiterstolean_task_imp(freed when the task completes). - Use a lazy thread-local
condition_variableviaMK_THREAD_LOCAL_GET_DEFand a stack-allocatedlean_task_waiternode linked ontoimp->m_waitersinwait_for. - In
resolve_core, detachimp->m_waitersand notify only waiters of that task while holdingm_mutex(beforehandle_finished, which may unlock forsync := truedependents). - Gate
m_task_finished_cv.notify_all()behind anm_wait_any_count > 0check forIO.waitAny(so even the 0-waiter fast path skipsnotify_all).
Measured on macOS arm64 (4.38.0-pre):
| Metric (10k unrelated resolutions, 32 sleeping waiters) | master |
With targeted waiters |
|---|---|---|
| Elapsed time | 68,020 µs | 912 µs (74.6× faster) |
Kernel sys time |
0.51 s | 0.01 s |
| Involuntary context switches | 21,069 | 184 (114× fewer) |
| CPU cycles | 1.29 B | 39.6 M |
Prototype branch with targeted waiters + regression test (prepared with AI assistance and reviewed/tested locally — 171/171 task/async/promise/wait/server tests passing):
https://github.com/nathanrs/lean4/compare/master...task-manager-targeted-wakeup
Impact
Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.
- Dominant language
- Lean
- Stars
- 10.5k
- Forks
- 1.2k
- Avg merge
- 19h 36m
- Merged PRs (30d)
- 323
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from leanprover/lean4
-
P-medium
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
leanprover/lean4#16958 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
leanprover/lean4#16897 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
leanprover/lean4#16892 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 86/100
leanprover/lean4#16886 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 78/100
leanprover/lean4#16820 ·
Maintainers usually reply within 1 day
All issues in leanprover/lean4
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
superfly/contextwindow#18 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Lincoln-cn/JAiRouter#1452 ·
Maintainers usually reply within 1 day
-
accepted
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
future-agi/future-agi#6351 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day