Skip to content

fix: crash when the environment is written while another thread reads it - #53491

Merged
MarshallOfSound merged 3 commits into
mainfrom
fix/linux-environ-thread-safe
Sep 4, 2026
Merged

MarshallOfSound merged 3 commits into
mainfrom
fix/linux-environ-thread-safe

Conversation

@MarshallOfSound

@MarshallOfSound MarshallOfSound commented Sep 4, 2026 •

Copy link
Copy Markdown
Member

Note: This PR also fixes a leak that the new spec exposed on the ASan shards. Native function callbacks set up in a Node.js worker were never freed when the worker exited, so the second commit frees them from an environment cleanup hook.

Description of Change

Before glibc 2.41, adding an environment variable reallocates environ and frees the old array. A getenv() on another thread can then read freed memory. This is the startup crash the Linux ASan shards keep hitting in fontconfig's getenv(): the fontconfig worker scans fonts while the main script adds variables.

  • shell/app/environ_linux.cc defines setenv, unsetenv, putenv and clearenv in the executable. Linux executables link with -rdynamic, so shared libraries (GTK, native addons) bind to these too.
  • A published environ array is never freed. Growing it doubles the capacity, copies the entries and publishes the new array. This is the scheme glibc 2.41 adopted upstream.
  • NAME=value strings are never freed and identical ones are reused, as in every glibc version.
  • Replaced arrays stay on a list so LeakSanitizer does not report them.
  • MSan builds keep glibc's functions.
  • On glibc 2.41 and later all four functions forward to glibc's own. Those are already safe, and their unsetenv and clearenv bump a private counter that getenv uses to retry, which the replacements can't do.
  • New Linux spec: a worker thread reads the environment through os.homedir() (libuv calls getenv() with no lock) while the main script adds 2000 variables.

Results on glibc 2.35, the version CI uses:

Build Without this change With this change
Testing, race repro 30 of 30 crashed in getenv() 30 of 30 passed
ASan, race repro 5 of 5 crashed in getenv() 10 of 10 passed
New spec, Testing 5 of 5 failed with SIGSEGV 5 of 5 passed

The extra memory is bounded by the peak variable count, not by the number of writes. Live heap, starting from 40 variables:

Pattern glibc This change
Add 3 variables 447 B 559 B
Add 1000 variables 39 KB 61 KB
Toggle one variable 100000 times 403 B 531 B
Set one variable to 100000 different values 3.19 MB 3.19 MB

macOS's libc has the same class of bug and is not covered here.

The new spec also exposed a leak, fixed in the second commit:

  • A Node.js worker's isolate has no gin::PerIsolateData, so the gin_helper::CallbackHolderBase objects created for Electron's bindings in every worker were never freed. V8 does not run weak callbacks when it disposes an isolate.
  • Those holders are now tracked per isolate and freed from a Node.js environment cleanup hook, which runs on the worker's thread before the isolate is disposed.

Checklist

  • PR description included
  • npm test passes
  • tests are added

Release Notes

Notes: Fixed a crash on Linux when process.env was written while another thread was reading the environment, and a memory leak when a worker thread exits.

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.
@MarshallOfSound MarshallOfSound added semver/patch backwards-compatible bug fixes target/43-x-y PR should also be added to the "43-x-y" branch. target/44-x-y PR should also be added to the "44-x-y" branch. target/45-x-y PR should also be added to the "45-x-y" branch. labels Sep 4, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Sep 4, 2026
@MarshallOfSound MarshallOfSound added the target/42-x-y PR should also be added to the "42-x-y" branch. label Sep 4, 2026
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.

@deepak1556 deepak1556 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I guess we can't make a change where we disallow mutating the native environment after process start and use a snapshotted env backend for JS mutations similar to Node.js https://github.com/nodejs/node/blob/f86053855de1e28e00b725acc7a34293e3b0e911/src/node_env_var.cc#L41-L70 ?

  2. Can we add a runtime check that this write shim is a noop for glibc >= 2.41, IIUC we don't need this workaround in 2.41 which has its own atomic read/write operations and since we aren't updating the shared counter https://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/getenv.c;h=4634387deb80259f6caf29266bf7b64131d8a840;hb=HEAD#l29 and it could pose a different issue.

  3. As a followup, we can drop the per isolate data dependency similar to https://chromium-review.googlesource.com/c/chromium/src/+/8279909 and that should avoid the need for a separate tracking of worker thread callbacks.

@MarshallOfSound

Copy link
Copy Markdown
Member Author
  1. Not really, people use this to actually modify behavior of for instance Chromium, or spawned child processes. And this change is IMO perfectly fine and retains existing behavior. Almost a pure win
  2. Yeah will check
  3. Yup noted as a follow up once that is pulled in / mirrored

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.
@MarshallOfSound

Copy link
Copy Markdown
Member Author
  1. You're right. unsetenv and clearenv bump __environ_counter on 2.41+ so getenv can retry a lookup that raced with a removal, and that counter isn't exported, so our unsetenv would let getenv miss a variable that's still set. Pushed a change so that on 2.41+ all four functions forward to glibc's own through dlsym(RTLD_NEXT, ...). I checked it by faking the version on a 2.35 box, and the race comes back, so the forwarding path is what runs.

@MarshallOfSound
MarshallOfSound enabled auto-merge (squash) September 4, 2026 05:03
@MarshallOfSound
MarshallOfSound merged commit fee963b into main Sep 4, 2026
74 of 80 checks passed
@MarshallOfSound
MarshallOfSound deleted the fix/linux-environ-thread-safe branch September 4, 2026 05:49
@release-clerk

release-clerk Bot commented Sep 4, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed a crash on Linux when process.env was written while another thread was reading the environment, and a memory leak when a worker thread exits.

@github-actions github-actions Bot added the 46-x-y label Sep 4, 2026
@trop

trop Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "44-x-y" cleanly;
you will need to perform this backport manually.

@trop trop Bot removed the target/44-x-y PR should also be added to the "44-x-y" branch. label Sep 4, 2026
@trop

trop Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "43-x-y" cleanly;
you will need to perform this backport manually.

@trop

trop Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "42-x-y" cleanly;
you will need to perform this backport manually.

@trop trop Bot removed the target/43-x-y PR should also be added to the "43-x-y" branch. label Sep 4, 2026
@trop

trop Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "45-x-y", please check out #53508

@trop

trop Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

@MarshallOfSound has manually backported this PR to "44-x-y", please check out #53509

@trop

trop Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

@MarshallOfSound has manually backported this PR to "43-x-y", please check out #53510

@trop

trop Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

@MarshallOfSound has manually backported this PR to "42-x-y", please check out #53511

MarshallOfSound added a commit that referenced this pull request Sep 4, 2026
… it (#53511)

fix: crash when the environment is written while another thread reads it (#53491)

* fix: crash when the environment is written while another thread reads it

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.

* fix: leak of native function callbacks when a worker thread exits

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.

* fix: use glibc's environment writers on glibc 2.41 and later

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.

(cherry picked from commit fee963b)
@trop trop Bot added merged/42-x-y PR was merged to the "42-x-y" branch. merged/45-x-y PR was merged to the "45-x-y" branch. and removed in-flight/42-x-y in-flight/45-x-y labels Sep 4, 2026
MarshallOfSound added a commit that referenced this pull request Sep 4, 2026
… it (#53510)

fix: crash when the environment is written while another thread reads it (#53491)

* fix: crash when the environment is written while another thread reads it

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.

* fix: leak of native function callbacks when a worker thread exits

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.

* fix: use glibc's environment writers on glibc 2.41 and later

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.

(cherry picked from commit fee963b)
@trop trop Bot added merged/43-x-y PR was merged to the "43-x-y" branch. and removed in-flight/43-x-y labels Sep 4, 2026
MarshallOfSound added a commit that referenced this pull request Sep 4, 2026
… it (#53509)

fix: crash when the environment is written while another thread reads it (#53491)

* fix: crash when the environment is written while another thread reads it

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.

* fix: leak of native function callbacks when a worker thread exits

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.

* fix: use glibc's environment writers on glibc 2.41 and later

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.

(cherry picked from commit fee963b)
@trop trop Bot added merged/44-x-y PR was merged to the "44-x-y" branch. and removed in-flight/44-x-y labels Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

46-x-y merged/42-x-y PR was merged to the "42-x-y" branch. merged/43-x-y PR was merged to the "43-x-y" branch. merged/44-x-y PR was merged to the "44-x-y" branch. merged/45-x-y PR was merged to the "45-x-y" branch. new-pr 🌱 PR opened recently semver/patch backwards-compatible bug fixes

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants