Std.Async.System: concurrent getEnvVar and setEnvVar calls can segfault

Aperta
#16,309 0 commenti 1 reazione 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
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Ambito
backend

Direzione di ricerca

Read src/Std/Async/System.lean around getEnvVar and setEnvVar, then inspect their native implementations in src/runtime/uv/system.cpp and the locking pattern in src/runtime/uv/event_loop.h. Reproduce the race with the supplied Lean program on Linux and check how environment access can be synchronized safely. Done means both tasks complete without a segmentation fault and lookups return none.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug
Description

Concurrent calls to Std.Async.System.getEnvVar and Std.Async.System.setEnvVar can reproducibly terminate Lean with SIGSEGV on Linux. These public APIs call uv_os_getenv and uv_os_setenv without synchronization, despite both libuv operations being documented as not thread-safe (eg uv_os_getenv). Repeatedly adding variables can reallocate the process-global environment array while another thread traverses it, leaving the reader accessing stale storage.

Context

This arose while checking concurrency behavior in the new Std.Async.System.getEnvVar and setEnvVar APIs (https://github.com/leanprover/lean4/blob/2d347a21fd2b6a90f0bc6448b8459d0219a2731a/src/Std/Async/System.lean#L245-L273) on Linux. Their native implementations (https://github.com/leanprover/lean4/blob/2d347a21fd2b6a90f0bc6448b8459d0219a2731a/src/runtime/uv/system.cpp#L250-L306) call libuv directly without acquiring a lock. This differs from event-loop operations, which are coordinated through event_loop_guard (https://github.com/leanprover/lean4/blob/2d347a21fd2b6a90f0bc6448b8459d0219a2731a/src/runtime/uv/event_loop.h#L23-L72).

Steps to Reproduce
  1. Save the following as EnvRace.lean:
  module

  import Std.Async.System

  /-! Reproduces concurrent environment access. -/

  public def main : IO Unit := do
    let start ← IO.Promise.new
    let writer ← IO.asTask (prio := .dedicated) do
      discard <| IO.wait start.result?
      for i in [0:8000] do
        Std.Async.System.setEnvVar s!"LEAN_ENV_RACE_{i}" "x"
    let reader ← IO.asTask (prio := .dedicated) do
      discard <| IO.wait start.result?
      for _ in [0:100000] do
        discard <| Std.Async.System.getEnvVar "LEAN_ENV_RACE_MISSING"
    start.resolve ()
    IO.ofExcept (← IO.wait writer)
    IO.ofExcept (← IO.wait reader)
  1. elan run leanprover/lean4:nightly-2026-10-27 lean --run EnvRace.lean
  2. Observe the segmentation fault

Expected behavior: Both tasks complete successfully; each lookup returns none and the process exits with status 0.

Actual behavior: Lean terminates with SIGSEGV inside libc getenv, reached through uv_os_getenv, and the command exits with status 139.

Versions

Lean (version 4.37.0-nightly-2026-10-27, x86_64-unknown-linux-gnu, commit 67bc4fe8130f9b5e1a073c73ed5cda09de6c63fe, Release)

Ubuntu 24.04.5 LTS, Linux 6.8.0-142-generic x86_64

Additional Information

Originally reported by @clairew from Trail of Bits during the Patch the Planet drive.

Impact

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
19h 8m
PR unite (30g)
421

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.