Repository navigation
[1/4] fix(heap): harden heap profiler lifecycle, translation, and OOM callbacks - #459
IlyasShabi wants to merge 1 commit into
Conversation
Overall package sizeSelf size: 2.8 MB Dependency sizes| name | version | self size | total size | |------|---------|-----------|------------| | pprof-format | 2.4.1 | 924.37 kB | 924.37 kB | | source-map | 0.8.0 | 185.66 kB | 185.66 kB | | node-gyp-build | 4.8.4 | 13.86 kB | 13.86 kB |🤖 This report was automatically generated by heaviest-objects-in-the-universe |
| auto& state = PerIsolateData::For(isolate)->GetHeapProfilerState(); | ||
| auto state = PerIsolateData::For(isolate)->GetHeapProfilerState(); |
There was a problem hiding this comment.
It's always a tiny alarm bell for me when you go from by-ref to by-value for a return value, but I understand why this is and it's actually safer this way. Maybe a one-line note saying the copy is deliberate to pin the object's lifetime, so nobody "fixes" it back to auto& later?
| if (typeof callback !== 'undefined') { | ||
| newCallback = (profile: AllocationProfileNode) => { | ||
| callback(convertProfile(profile)); | ||
| // Near-OOM capture always translates live-only AllocationProfileNode |
There was a problem hiding this comment.
they aren't really "live-only" but since you're fixing this in #460 it should be fine. If I want to be pedantic, you might remove that wording until then from here.
| static std::mutex g_heap_profiler_mutex; | ||
|
|
||
| // Cleanup hook to stop heap profiler before isolate is destroyed | ||
| static void HeapProfilerCleanupHook(void* data) { |
There was a problem hiding this comment.
Because you removed the hook, StopSamplingHeapProfiler now runs inside ~HeapProfilerState, while the PerIsolateData cleanup lambda holds a mutex. If a worker thread exits while still profiling, other isolates calling PerIsolateData::For will block until teardown finishes. Any re-entry into For() from that path would deadlock, although I'm not sure if there are any such reentries.
Fix ideas (suggested by Claude, so take with grain of salt) are to take the state out of the map, or stop sampling, then destroy it after the lock_guard is released.
There was a problem hiding this comment.
I looked into this more, and I couldn't find anything on that path that re-enters PerIsolateData::For, so I don't think it deadlocks. But in allocation mode, stopping frees every retained sample, and every other isolate's For() (profile(), NearHeapLimit, the wall profiler) blocks meanwhile, and it might take some time. Later in #462 you added a test with 4 workers exiting this way.
Could we move the state out under the lock and destroy it after releasing it? Something like:
std::shared_ptr<HeapProfilerState> doomed;
{
const std::lock_guard<std::mutex> lock(mutex);
auto it = per_isolate_data_.find(isolate);
if (it != per_isolate_data_.end()) {
doomed = std::move(it->second.GetHeapProfilerState());
per_isolate_data_.erase(it);
}
}
// doomed (and the V8 sampler stop) released here, outside the lock| static std::mutex g_heap_profiler_mutex; | ||
|
|
||
| // Cleanup hook to stop heap profiler before isolate is destroyed | ||
| static void HeapProfilerCleanupHook(void* data) { |
There was a problem hiding this comment.
You have a comment in bindings/profilers/near-oom.cc:347 that still references this method name.
| heapProfiler.start(64 * 1024, 64, false); | ||
| const p = heapProfiler.profile(); | ||
| assert.ok(p.sampleType.length === 2); | ||
| heapProfiler.stop(); |
There was a problem hiding this comment.
this should be in a finally block of a try/finally or an assertion failure will cause other tests to fail with "profiler already started"
| assert.ok(retained.length === 16); | ||
| const root = heapProfiler.v8Profile(); | ||
| assert.ok(Array.isArray(root.children)); | ||
| heapProfiler.stop(); |
There was a problem hiding this comment.
this should be in the finally block below or an assertion failure will cause other tests to fail with "profiler already started"
|
|
||
| uint64_t sample_interval = info[0].As<v8::Integer>()->Value(); | ||
| if (sample_interval == 0) { | ||
| return Nan::ThrowRangeError("Sample interval must be greater than 0."); |
There was a problem hiding this comment.
This is very correct to add here, but it can have an… interesting side effect, namely:
start() sets startedWithAllocations, heapIntervalBytes and heapStackDepth before it calls here and it can now throw RangeError when intervalBytes is 0, and the values start() set stay set. A later call to the exported convertProfile() then runs in the wrong mode.
This isn't a new problem, just your new RangeError path makes it easier to hit.
There was a problem hiding this comment.
I see you fixed this in #462 so probably nothing to do here.
Stack
mainishabi/heap-pruner-1-lifecycle-perfBuildAllocationStatsByNodeIdand live-only near-OOM filteringishabi/heap-pruner-2-watermark-oomSamplingHeapPrunerimplementation + design docishabi/heap-pruner-3-sampling-prunerSamplingHeapPruner, remove restart loop, full integration & worker testsMotivation
Prepares the heap profiler codebase for continuous in-place allocation profiling (DataDog/dd-trace-js#11122) by fixing standalone lifecycle, correctness, and translation issues that are valuable independently of the pruner.
Changes
bindings/profilers/heap.cc,bindings/profilers/near-oom.hh):HeapProfilerCleanupHook+ global mutex/set (relying onPerIsolateData's existing environment cleanup hook to destroyHeapProfilerState).stoppedguard inHeapProfilerStateso a stale~HeapProfilerStatekept alive by an in-flight OOM/interrupt callback cannot stop a newly restarted session.sample_interval == 0withRangeErrorwithout wedging state.statestd::shared_ptrby value inGetAllocationProfile()and scopedallocation_stats.bindings/profile-translator.hh,bindings/allocation-profile.{hh,cc},bindings/translate-heap-profile.cc):CreateDataPropertysoObject.prototypesetters cannot run or throw during translation.AllocationStatKeys(kInternalizedliterals), added single-bucket fast path, deterministicSizeEntrysort, innerHandleScope/EscapableHandleScope, single-pass branchlessBuildAllocationStatsByNodeId, and 64-bit byte accumulation.(external)fixes (ts/src/heap-profiler.ts):monitorOutOfMemory()callbacks to always convert near-OOM profiles in live-only mode (allocations = false).allocObjects: 0, allocSpaceBytes: 0on the synthetic(external)live gauge node in allocation mode.bindings/test/binding.cc,ts/test/test-heap-profiler.ts,ts/test/oom-heap-limit-extension.ts):TranslateAllocationStatsand 64-bit overflow protection.intervalBytes === 0,Object.prototypesetter immunity, and OOM Async/Interrupt/restart-in-callback flows.