Default worker count ignores CPU affinity and cgroup CPU quotas on Linux
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 34/100
- Issue type
- Feature
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- linux
- Domain
- operating-systems
Research direction
Start with runtime/object.cpp and Lean/Shell.lean, which the issue names as the places setting the current defaults; compare those paths with the described LEAN_NUM_THREADS behavior. The proposed change also touches Linux CPU affinity and cgroup v1/v2 quota detection, plus the chunk count for importedRelevantConstants. The issue says the patch has tests; done means defaults reflect visible CPU limits while explicit -j and LEAN_NUM_THREADS still take precedence.
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 the latest nightly release (
nightly-2026-11-05). - When reporting a perceived soundness bug: not applicable.
Description
On Linux, the default number of worker threads is std::thread::hardware_concurrency(), the number of logical CPUs of the machine. This is used by the lean executable and by every compiled Lean program, including Lake. It ignores both the CPU affinity of the process and any cgroup CPU quota.
On my test host, lean defaults to 255 workers when pinned to 4 CPUs with taskset, and also when running under a cgroup quota of 24 CPUs. In a container or batch job this oversizes every pool, and lake build multiplies the effect by starting one compiler process per ready module.
Context
- #4241 asks for a
-joption in Lake. This issue is about the default used when nothing is set, and does not ask for a new option. - #13856 adds an opt-in cross-process limit based on a POSIX semaphore. It is complementary: it bounds the total across processes, whereas this issue concerns the size of each process's default pool.
- #13767 reports resource pressure from nested Lake and compiler concurrency on macOS. This issue is Linux-specific and does not claim to address it.
LEAN_NUM_THREADS works around the problem for compiled programs such as Lake (see this Zulip thread). It does not change the pool of the lean executable, which is set by -j and otherwise defaults to the hardware concurrency. Lake does not pass -j to the compilers it starts.
Steps to Reproduce
Pin the process to four CPUs that are actually allowed, and print the default:
cpus=$(python3 -c 'import os; print(",".join(map(str, sorted(os.sched_getaffinity(0))[:4])))')
cat > Defaults.lean <<'LEAN'
module
import all Lean.Shell
public def main : IO Unit :=
IO.println s!"hardware={System.Platform.Internal.getHardwareConcurrency ()} shell_default={Lean.defaultNumThreads}"
LEAN
taskset -c "$cpus" lean --run Defaults.lean
For a Lake build, generate a project with sixteen independent modules that each do a fixed amount of work at elaboration time:
from pathlib import Path
Path('P').mkdir()
Path('lakefile.toml').write_text('name = "lakeprobe"\n[[lean_lib]]\nname = "P"\n')
Path('P.lean').write_text(''.join(f'import P.M{i}\n' for i in range(16)))
for i in range(16):
Path(f'P/M{i}.lean').write_text(f'''def burn{i} (n : Nat) : Nat := Id.run do
let mut acc := 0
for i in [0:n] do
acc := (acc * 31 + i) % 1000000007
return acc
#eval burn{i} 1000000
''')
Run taskset -c "$cpus" lake build --no-cache +P:leanArts in a fresh copy of this project, once as is and once with LEAN_NUM_THREADS=4, and count the processes whose command line contains --setup (the compilers).
Expected behavior: The default worker count reflects the CPUs the process may use. An explicit -j or LEAN_NUM_THREADS still takes precedence.
Actual behavior: hardware=255 shell_default=255 with four CPUs allowed.
Versions
- Lean 4.37.0-nightly-2026-11-05 (
67dafde474fbaf278d6d7f822068549bf6e532fa); the same defaults are in 4.35.0 and in current master (Lean/Shell.lean, andget_lean_num_threadsinruntime/object.cpp). - Linux x86-64, cgroup v1, 255 logical CPUs, a visible cgroup quota of 24 CPUs.
Additional Information
Peak number of concurrent compiler processes for the Lake project above with four CPUs allowed (nightly-2026-11-05, wall time of one run):
| Setting | Compilers | Wall (s) |
|---|---|---|
| default | 16 | 2.32 |
LEAN_NUM_THREADS=4 |
4 | 2.32 |
LEAN_NUM_THREADS=2 |
2 | 4.16 |
On this small project the default is as fast as four workers, so I am not claiming a speedup: the difference is the number of processes, and the memory they hold.
I plan to fix this by adding a separate System.Platform.Internal.getAvailableParallelism, leaving getHardwareConcurrency unchanged. On Linux it returns the minimum of the calling thread's CPU affinity and the CPU quotas of the process's cgroup and all of its ancestors that are visible in the mount namespace (cgroup v1 and v2). A fractional quota is rounded up, so the result is never below one. Other platforms keep returning the hardware concurrency. It would be used for the default of the lean executable, for the default of compiled programs (when LEAN_NUM_THREADS is unset), and for the chunk count in library suggestions' importedRelevantConstants. Explicit settings are untouched.
The result is a default pool size, not a cap: Task.get compensation and dedicated tasks can still create more threads. Limits that are not visible from inside the container cannot be detected and are not guessed. The value is computed once at startup, as it is today.
I have a patch with tests for this and would be glad to submit it. Since the repository only accepts PRs from a limited set of contributors, how would you like me to proceed if the approach looks reasonable?
AI assistance: Codex and Claude Code helped with the investigation, the patch and this text. I have reviewed the code and the results myself.
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.3k
- Forks
- 1.1k
- Avg merge
- 19h 8m
- Merged PRs (30d)
- 421
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
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
leanprover/lean4#16681 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
leanprover/lean4#16305 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
leanprover/lean4#16299 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
leanprover/lean4#16257 ·
Maintainers usually reply within 1 day
-
`IO.FS.realPath` reports every failure as `noFileOrDirectory` instead of classifying by `errno`Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
leanprover/lean4#15683 ·
Maintainers usually reply within 1 day
All issues in leanprover/lean4
Similar issues
-
agent:WSL code-review linux pri:medium refactoring
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
gaming: omarchy-remove-gaming-battlenet never stops the running installer, so Battle.net reinstalls itself after removalPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
AvengeMedia/dank-greeter#56 ·
-
sys_full_path on Windows copies only half of each path part's case on diskPossibly taken @dimensionscape claimed this 1 day ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 85/100
HaxeFoundation/hashlink#1026 ·
Maintainers usually reply within 1 day
-
stdlib topic-subprocess type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day