Repository navigation
fix: crash when the environment is written while another thread reads it - #53508
Merged
Conversation
Before glibc 2.41, adding an environment variable reallocates the environ array and frees the old one, so a getenv() on another thread can read freed memory. Chromium's thread pool and system libraries call getenv() at any time (the Linux ASan shards have been crashing in fontconfig's getenv() at startup), and apps add variables through process.env whenever they like. Define setenv(), unsetenv(), putenv() and clearenv() in the executable so they replace glibc's for the whole process. They follow the scheme glibc 2.41 adopted: a published environ array is never freed, and "NAME=value" strings are never freed, as in every glibc version. Replaced arrays are kept on a list so leak checkers do not report them. Co-authored-by: Sam Attard <[email protected]>
gin_helper::CallbackHolderBase is freed either by a V8 weak callback or when gin reports that its isolate is being disposed. A Node.js worker's isolate has no gin::PerIsolateData, so gin never reports it, and V8 does not run weak callbacks when it disposes an isolate. Every native function a worker set up (Electron's bindings are installed in each worker) therefore leaked when the worker exited. Track the holders of isolates without gin::PerIsolateData and free them from a Node.js environment cleanup hook. The hook runs on the worker's thread after its JavaScript has ended and before the isolate is disposed. The environ race fixture now waits for its worker to exit, so LeakSanitizer checks the worker's teardown. Co-authored-by: Sam Attard <[email protected]>
glibc 2.41 made getenv() safe against concurrent writers itself. Its unsetenv() and clearenv() also bump a private counter that getenv() checks to retry a lookup that raced with a removal. The replacements cannot bump that counter, so on those versions a getenv() could miss a variable that is still set. Check gnu_get_libc_version() on the first call and forward all four functions to glibc's own when it is 2.41 or later. Co-authored-by: Sam Attard <[email protected]>
3 tasks done
MarshallOfSound
approved these changes
Sep 4, 2026
MarshallOfSound
enabled auto-merge (squash)
September 4, 2026 05:51
MarshallOfSound
deleted the
trop/45-x-y-bp-fix-crash-when-the-environment-is-written-while-another-thread-reads-it-1788500999411
branch
September 4, 2026 07:26
|
Release Notes Persisted
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport of #53491
See that PR for details.
Notes: Fixed a crash on Linux when
process.envwas written while another thread was reading the environment, and a memory leak when a worker thread exits.