From 403f34b144fcda38a37f8428600909cf0428917f Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 27 Jul 2021 10:06:13 -0700 Subject: [PATCH] perf_hooks: fix PerformanceObserver gc crash Signed-off-by: James M Snell Fixes: https://github.com/nodejs/node/issues/39548 --- lib/internal/perf/observe.js | 1 + test/parallel/test-perf-gc-crash.js | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 test/parallel/test-perf-gc-crash.js diff --git a/lib/internal/perf/observe.js b/lib/internal/perf/observe.js index af09d33c5405f3..677d4f4c617ce9 100644 --- a/lib/internal/perf/observe.js +++ b/lib/internal/perf/observe.js @@ -130,6 +130,7 @@ function maybeDecrementObserverCounts(entryTypes) { if (observerType === NODE_PERFORMANCE_ENTRY_TYPE_GC && observerCounts[observerType] === 0) { removeGarbageCollectionTracking(); + gcTrackingInstalled = false; } } } diff --git a/test/parallel/test-perf-gc-crash.js b/test/parallel/test-perf-gc-crash.js new file mode 100644 index 00000000000000..d980e91a2f2799 --- /dev/null +++ b/test/parallel/test-perf-gc-crash.js @@ -0,0 +1,25 @@ +'use strict'; + +require('../common'); + +// Refers to https://github.com/nodejs/node/issues/39548 + +// The test fails if this crashes. If it closes normally, +// then all is good. + +const { + PerformanceObserver, +} = require('perf_hooks'); + +// We don't actually care if the observer callback is called here. +const gcObserver = new PerformanceObserver(() => {}); + +gcObserver.observe({ entryTypes: ['gc'] }); + +gcObserver.disconnect(); + +const gcObserver2 = new PerformanceObserver(() => {}); + +gcObserver2.observe({ entryTypes: ['gc'] }); + +gcObserver2.disconnect();