Concurrent `Job.bindM` callbacks race on `BuildStore` in `fetchOrCreate`, launching duplicate concurrent builds of shared plugin modules
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
- 35/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Ambito
- build-system
Direzione di ricerca
Start with src/lake/Lake/Util/Store.lean and src/lake/Lake/Build/Index.lean to trace fetchOrCreate, then read src/lake/Lake/Build/Target/Fetch.lean and src/lake/Lake/Build/Job/Monad.lean for the concurrent callers. Reproduce with two concurrent fetches of the same memoized facet and inspect the build output for duplicate compiler launches. Done means concurrent requests share one in-flight build; add a deterministic regression test.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
On Lean and Lake v4.36.0 (611c117bfaf7b7ae1cfcf246ce6fba31e8f509e9), when many modules in a library declare the same shared plugin (plugins = ["<PluginLib>:shared"]), Lake can launch two concurrent lean compiler processes for the exact same plugin module, reading the same source file and writing to the same .olean / .setup.json / .c output paths while Lake still exits with code 0.
The root cause is that PartialBuildKey.fetchInCoreAux (Target/Fetch.lean:65) uses Job.bindM with the default sync := false, dispatching concurrent callbacks that share the same IO.Ref BuildStore (Job/Monad.lean:245,250), while recBuildWithIndex → fetchOrCreate (Index.lean:48, Util/Store.lean:47-55) performs an unsynchronized check-create-insert (fetch? → create → store) across an await boundary (Library.lean:141).
Observed Evidence on Lean 4.36.0
During a single lake build ArkLib on frozen ArkLib (2934c834177ee4de50b9df5ce58509a8529fbf88, 1,252 owned modules, LEAN_NUM_THREADS=8, Lean -j2) using an official-recipe Stage 2 Lean 4.36.0 build (611c117bfaf7b7ae1cfcf246ce6fba31e8f509e9):
- The
lakefile.tomldefines a singleArkLibLintPluginlibrary, referenced byArkLibviaplugins = ["ArkLibLintPlugin:shared"](the only other reference is in the unrequestedArkLibTestlibrary). - The single Lake root process (PID
51393) spawned 1,253 physicalleanprocesses for 1,252 unique owned modules. ArkLibLintPluginwas compiled twice concurrently:
| Field | Attempt 1 | Attempt 2 |
|---|---|---|
| PID | 51522 |
51525 |
| Launch delta | 0 ms |
+4.895 ms |
| Execution overlap | 453.018 ms |
453.018 ms |
| Process CPU (user + sys) | 496.335 ms |
528.342 ms |
| Source SHA-256 | 3e0a92ef47b5b608199f130701842a8f9e4b7fb8902a9be50bd0a38c9a3d33c6 |
identical |
| Setup path | .lake/build/ir/ArkLibLintPlugin.setup.json |
identical |
Lake exited with status 0 despite both processes concurrently writing to the same shared output paths.
Source-Level Root Cause (611c117bfaf7b7ae1cfcf246ce6fba31e8f509e9)
- Every consumer module fetches the shared plugin via
PartialBuildKey.fetchInCoreAux.Module.recFetchPreSetup(src/lake/Lake/Build/Module.lean:650) callsmod.plugins.fetchIn mod.pkg "module plugins"for each module in the library.- For
.facet target shortFacet(e.g.ArkLibLintPlugin:shared),PartialBuildKey.fetchInCoreAux(src/lake/Lake/Build/Target/Fetch.lean:54-67) resolves the target job (Job.purefor alean_libconfig target fromIndex.lean:38) and then calls:let job ← (job.cast h).bindM (kind := cfg.outKind) fun data => fetch (.facet target kind data facet)
Job.bindMruns thefetchcallback asynchronously on the sharedIO.Ref BuildStore.Job.bindM(src/lake/Lake/Build/Job/Monad.lean:240-256) defaults tosync := falseand passes the sharedstorereference intoBaseIO.bindTask:BaseIO.bindTask task (prio := prio) (sync := sync) fun | .ok a sa => do ... match (← withLoggedIO (f a) |>.toFn fetch pkg? stack store ctx {sa with trace}) with- Consequently, multiple consumer modules' plugin fetches execute
fetch (.facet <ArkLibLintPlugin> lean_lib data lean_lib.shared)concurrently on separate worker threads against the sameBuildStore.
fetchOrCreatehas no synchronization or in-flight placeholder acrosscreate.recBuildWithIndex(src/lake/Lake/Build/Index.lean:46-48) memoizes facets viafetchOrCreate key recFetch.fetchOrCreate(src/lake/Lake/Util/Store.lean:47-55) performs an unsynchronized check-then-act sequence:@[inline] public def fetchOrCreate [Monad m] (key : κ) [MonadStore1Of key α m] (create : m α) : m α := do if let some val ← fetch? key then return val else let val ← create store key val return val- For
LeanLib.sharedFacetConfig(src/lake/Lake/Build/Library.lean:139-168),recBuildSharedawaitsself.modules.fetchat line 141 andmod.transImports.fetchat line 150 beforerecBuildSharedreturns andstore key valinserts thesharedfacet job intoBuildStore. - Any second async
bindMworker that reachesfetch? keyduring this wide window also observesnone, entersLeanLib.recBuildShared, and recursively callsfetchOrCreatefor the plugin module's native/elaboration facets (elabArts,Module.lean:1248) concurrently, spawning a duplicateleancompilation on the same output files.
Expected Behavior and Suggested Fix
Lake should guarantee that a memoized BuildKey is created at most once per build, even when fetch is invoked from concurrent Job.bindM callbacks:
- Root fix (atomic memoization in
BuildStore/fetchOrCreate): GuardBuildStorelookup/insertion (e.g., via a mutex or an in-flightIO.Promise/placeholder entry keyed byBuildKey) so concurrentfetchOrCreatecallers for the same key share the in-flight job instead of runningcreatetwice. - Targeted mitigation in
Target/Fetch.lean:65: For already-resolved target jobs (such asJob.pureconfig targets), avoid unnecessarily hopping onto an async worker thread viabindM (sync := false)before callingfetch (.facet ...). (Note: any otherJob.bindMcaller invokingfetchstill requires fix #1 for general soundness.)
Reproducibility Note
Because the race requires two Job.bindM callbacks to overlap inside fetchOrCreate before the first store completes, small low-concurrency projects (e.g. 8 consumers at LEAN_NUM_THREADS=2) rarely hit the window in a single run, whereas larger libraries with many consumers sharing a :shared plugin at higher LEAN_NUM_THREADS (e.g. ArkLib at LEAN_NUM_THREADS=8) trigger it cleanly. A deterministic unit test can directly invoke two concurrent Job.bindM (sync := false) tasks calling fetch on the same memoized facet whose fetchFn yields/awaits before returning.
- 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
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Unable to installAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
[BUG] Nightly Build JDK 17: latest-frameworks job red since 2026-11-28 — quarkiverse-langchain4j 2.0.0 pins Quarkus 4.0.0, which requires Java 21Forse già presa @uhhgxg l’ha presa oggi. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 95/100
langchain4j/langchain4j#7344 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
connectrpc/connect-es#1915 ·
I maintainer di solito rispondono entro 1 giorno
-
ninja_build.sh -t compile_commands fails: `cmake ..` points at build/ instead of the repo rootApertaneeds review regression MS4
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 95/100
I maintainer di solito rispondono entro 2 giorni