Repository navigation
fix: don't allocate crash keys inside the v8 oom callback - #52591
Conversation
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.
deepak1556
left a comment
There was a problem hiding this comment.
The fixed heap stat keys will now duplicate annotations with
electron/shell/renderer/oom_stack_trace.cc
Lines 154 to 202 in 41bb886
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:
-
Main thread only: simplest and follows Blink, but loses worker OOM diagnostics.
-
Unrestricted fixed keys (this PR apporach): usually captures the correct worker, but has a concurrent writer race.
-
Fixed keys plus atomic ownership: capture the first failing main/worker isolate and avoid mixed concurrent writes via the atomic flag.
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.
|
@deepak1556 fixed this up per option 3! |
| if (g_claimed.compare_exchange_strong(expected, true)) { | ||
| g_owner.store(isolate); |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Needs a lint fix additionally, but LGTM otherwise
ckerr
left a comment
There was a problem hiding this comment.
Agree with Deepak's comment, but othewise LGTM.
(TIL SafeSPrintf explicitly wants %d instead of %zu for size_t)
|
CC @alexkozy who wrote the code. |
|
Release Notes Persisted
|
|
I have automatically backported this PR to "42-x-y", please check out #52723 |
|
I have automatically backported this PR to "44-x-y", please check out #52724 |
|
I have automatically backported this PR to "43-x-y", please check out #52725 |
Description of Change
V8OOMErrorCallbackrecords fiveelectron.v8-oom.*crash keys throughelectron::crash_keys::SetCrashKey, which grows twostd::deques and buildsstd::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 insideSetCrashKey(EXC_BAD_INSTRUCTION / EXC_BREAKPOINT) instead of atOOM_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 withbase::strings::SafeSPrintf, so nothing on the OOM path allocates.Checklist
npm testpassesRelease 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.