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

Open
#16,309 0 comments 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
backend

Research direction

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.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Lean
Stars
10.3k
Forks
1.1k
Avg merge
19h 8m
Merged PRs (30d)
421

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.