Skip to content

fix: don't allocate crash keys inside the v8 oom callback - #52591

Merged
MarshallOfSound merged 6 commits into
mainfrom
v8-key-oom
Aug 9, 2026
Merged

MarshallOfSound merged 6 commits into
mainfrom
v8-key-oom

Conversation

@codebytere

@codebytere codebytere commented Aug 2, 2026 •

Copy link
Copy Markdown
Member

Description of Change

V8OOMErrorCallback records five electron.v8-oom.* crash keys through electron::crash_keys::SetCrashKey, which grows two std::deques and builds std::strings. That is allocation inside V8's out-of-memory handler, and the callback can fire on a V8 worker thread while the main thread is mid-SetCrashKey. In crash reports this shows up as processes dying inside SetCrashKey (EXC_BAD_INSTRUCTION / EXC_BREAKPOINT) instead of at OOM_CRASH(), so the report loses the OOM signature it was trying to record.

Swap those five keys to statically registered fixed-storage crash_reporter::CrashKeyStrings and format the heap stats into a stack buffer with base::strings::SafeSPrintf, so nothing on the OOM path allocates.

Checklist

  • PR description included and stakeholders cc'd
  • npm test passes
  • relevant documentation, tutorials, templates and examples are changed or are not needed

Release Notes

Notes: Fixed an issue where a JavaScript out-of-memory crash could be reported from inside the crash-key handler instead of at the out-of-memory site.

V8OOMErrorCallback records five electron.v8-oom.* crash keys through
electron::crash_keys::SetCrashKey, which grows two std::deques and
builds std::strings - allocation inside V8's out-of-memory handler,
which can also fire on a V8 worker thread. In crash reports this shows
up as the process dying inside SetCrashKey instead of at OOM_CRASH(),
losing the OOM signature the keys were meant to record.

Use statically registered fixed-storage crash_reporter::CrashKeyStrings
and format the heap stats into a stack buffer with
base::strings::SafeSPrintf, so nothing on the OOM path allocates.
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Aug 2, 2026
@codebytere codebytere added semver/patch backwards-compatible bug fixes target/42-x-y PR should also be added to the "42-x-y" branch. 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. labels Aug 2, 2026
@codebytere
codebytere marked this pull request as draft August 2, 2026 09:18
@codebytere
codebytere marked this pull request as ready for review August 3, 2026 02:16

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

The fixed heap stat keys will now duplicate annotations with

crash_keys::SetCrashKey("electron.v8-oom.stack",
heap_info + " (stack pending)");
crash_keys::SetCrashKey("electron.v8-oom.heap.used",
base::NumberToString(stats.used_heap_size()));
crash_keys::SetCrashKey("electron.v8-oom.heap.total",
base::NumberToString(stats.total_heap_size()));
crash_keys::SetCrashKey("electron.v8-oom.heap.limit",
base::NumberToString(stats.heap_size_limit()));
crash_keys::SetCrashKey("electron.v8-oom.heap.total_available",
base::NumberToString(stats.total_available_size()));
crash_keys::SetCrashKey("electron.v8-oom.heap.total_physical",
base::NumberToString(stats.total_physical_size()));
crash_keys::SetCrashKey("electron.v8-oom.heap.malloced_memory",
base::NumberToString(stats.malloced_memory()));
crash_keys::SetCrashKey("electron.v8-oom.heap.external_memory",
base::NumberToString(stats.external_memory()));
crash_keys::SetCrashKey(
"electron.v8-oom.heap.native_contexts",
base::NumberToString(stats.number_of_native_contexts()));
crash_keys::SetCrashKey(
"electron.v8-oom.heap.detached_contexts",
base::NumberToString(stats.number_of_detached_contexts()));
double utilization = stats.heap_size_limit() > 0
? static_cast<double>(stats.used_heap_size()) /
stats.heap_size_limit() * 100.0
: 100.0;
crash_keys::SetCrashKey("electron.v8-oom.heap.utilization_pct",
absl::StrFormat("%.1f", utilization));
v8::HeapSpaceStatistics space_stats;
for (size_t i = 0; i < isolate->NumberOfHeapSpaces(); i++) {
isolate->GetHeapSpaceStatistics(&space_stats, i);
if (std::string_view(space_stats.space_name()) == "old_space") {
crash_keys::SetCrashKey(
"electron.v8-oom.old_space.used",
base::NumberToString(space_stats.space_used_size()));
crash_keys::SetCrashKey("electron.v8-oom.old_space.size",
base::NumberToString(space_stats.space_size()));
} else if (std::string_view(space_stats.space_name()) ==
"large_object_space") {
crash_keys::SetCrashKey(
"electron.v8-oom.lo_space.used",
base::NumberToString(space_stats.space_used_size()));
crash_keys::SetCrashKey("electron.v8-oom.lo_space.size",
base::NumberToString(space_stats.space_size()));
}
}
. I don't think allocation is the issue here, they don't belong to the V8 managed heap. The thread safety issue is real, it is also probably why Blink only records these keys for its main thread.

Since NearHeapLimitCallback is also registered on worker isolates and writes through the same process global registry, fixing only V8OOMErrorCallback does not remove the cross thread race. We should either restrict all V8 heap stat crash key writes to the main isolate similar to Blink or migrate the complete OOM key set to a shared fixed storage owner to avoid duplications.

However, even with the fixed storage concurrent writes will race (multiple threads OOM) and record possibly mixed data since crashpad doesn't guarantee safety.

So the options are:

  1. Main thread only: simplest and follows Blink, but loses worker OOM diagnostics.

  2. Unrestricted fixed keys (this PR apporach): usually captures the correct worker, but has a concurrent writer race.

  3. Fixed keys plus atomic ownership: capture the first failing main/worker isolate and avoid mixed concurrent writes via the atomic flag.

    cc @alexkozy from #50043 that introduced these crash keys.

V8OOMErrorCallback recorded its electron.v8-oom.* keys through
electron::crash_keys::SetCrashKey, whose registry grows two std::deques
and is written from whichever thread reports the OOM, so a V8 worker
thread races the main thread inside SetCrashKey. The renderer's
near-heap-limit callback writes the same key names through the same
registry from every isolate that registers it, so the race is
cross-thread even without the OOM callback, and fixed keys added only to
the OOM path would duplicate its annotations.

Give the whole electron.v8-oom.* set one owner (statically registered
crash_reporter::CrashKeyString storage in shell/common/v8_oom_diagnostics)
shared by both callbacks, with an atomic first claim: the first isolate
to hit an OOM records its diagnostics and later isolates return without
writing, so concurrent OOMs cannot interleave and worker diagnostics are
kept. The SetCrashKey path is removed from both call sites.
@codebytere-ant

Copy link
Copy Markdown

@deepak1556 fixed this up per option 3!

@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Aug 4, 2026
Comment thread shell/common/v8_oom_diagnostics.cc Outdated
Comment on lines +29 to +30
if (g_claimed.compare_exchange_strong(expected, true)) {
g_owner.store(isolate);

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.

When does null isolate case happen ? But having two separate atomic ops is problematic. An option would be,

char g_no_isolate;
std::atomic<const void*> g_owner{nullptr};

bool ClaimFor(v8::Isolate* isolate) {
  const void* current = isolate ? static_cast<const void*>(isolate)
                                : static_cast<const void*>(&g_no_isolate);
  const void* expected = nullptr;

  if (g_owner.compare_exchange_strong(expected, current)) {
    return true;
  }

  return expected == current;
}

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

Needs a lint fix additionally, but LGTM otherwise

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

Agree with Deepak's comment, but othewise LGTM.

(TIL SafeSPrintf explicitly wants %d instead of %zu for size_t)

@nikwen

nikwen commented Aug 5, 2026

Copy link
Copy Markdown
Member

CC @alexkozy who wrote the code.

@MarshallOfSound
MarshallOfSound merged commit 59fe77f into main Aug 9, 2026
75 checks passed
@MarshallOfSound
MarshallOfSound deleted the v8-key-oom branch August 9, 2026 03:05
@release-clerk

release-clerk Bot commented Aug 9, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed an issue where a JavaScript out-of-memory crash could be reported from inside the crash-key handler instead of at the out-of-memory site.

@trop

trop Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot added in-flight/42-x-y and removed target/42-x-y PR should also be added to the "42-x-y" branch. labels Aug 9, 2026
@trop

trop Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

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

@trop

trop Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot added in-flight/44-x-y in-flight/43-x-y merged/42-x-y PR was merged to the "42-x-y" branch. merged/44-x-y PR was merged to the "44-x-y" branch. merged/43-x-y PR was merged to the "43-x-y" branch. and removed target/44-x-y PR should also be added to the "44-x-y" branch. target/43-x-y PR should also be added to the "43-x-y" branch. in-flight/42-x-y in-flight/44-x-y in-flight/43-x-y labels Aug 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

45-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. semver/patch backwards-compatible bug fixes

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

6 participants