Skip to content

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

Merged
MarshallOfSound merged 3 commits into
45-x-yfrom
trop/45-x-y-bp-fix-crash-when-the-environment-is-written-while-another-thread-reads-it-1788500999411
Sep 4, 2026
Merged

MarshallOfSound merged 3 commits into
45-x-yfrom
trop/45-x-y-bp-fix-crash-when-the-environment-is-written-while-another-thread-reads-it-1788500999411

Conversation

@trop

@trop trop Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Backport of #53491

See that PR for details.

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.

trop Bot and others added 3 commits September 4, 2026 05:50
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]>
@trop
trop Bot requested a review from MarshallOfSound September 4, 2026 05:50
@trop trop Bot added 45-x-y backport This is a backport PR semver/patch backwards-compatible bug fixes labels Sep 4, 2026
@MarshallOfSound
MarshallOfSound enabled auto-merge (squash) September 4, 2026 05:51
@MarshallOfSound MarshallOfSound self-assigned this Sep 4, 2026
@MarshallOfSound
MarshallOfSound merged commit f586466 into 45-x-y Sep 4, 2026
95 checks passed
@MarshallOfSound
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-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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

45-x-y backport This is a backport PR semver/patch backwards-compatible bug fixes

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant