Skip to content

Commit

Permalink
perf_hooks: fix PerformanceObserver 'gc' crash
Browse files Browse the repository at this point in the history
Signed-off-by: James M Snell <[email protected]>
Fixes: #38412

PR-URL: #38414
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Zijian Liu <[email protected]>
  • Loading branch information
jasnell authored and targos committed Apr 29, 2021
1 parent 4f6c4eb commit 013fa59
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/internal/perf/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const kDeprecationMessage =
const kTypeSingle = 0;
const kTypeMultiple = 1;

let gcTrackingInstalled = false;

const kSupportedEntryTypes = ObjectFreeze([
'function',
'gc',
Expand Down Expand Up @@ -124,8 +126,11 @@ function maybeIncrementObserverCount(type) {

if (observerType !== undefined) {
observerCounts[observerType]++;
if (observerType === NODE_PERFORMANCE_ENTRY_TYPE_GC)
if (!gcTrackingInstalled &&
observerType === NODE_PERFORMANCE_ENTRY_TYPE_GC) {
installGarbageCollectionTracking();
gcTrackingInstalled = true;
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-performanceobserver-gc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

require('../common');

// Verifies that setting up two observers to listen
// to gc performance does not crash.

const {
PerformanceObserver,
} = require('perf_hooks');

// We don't actually care if the callback is ever invoked in this test
const obs = new PerformanceObserver(() => {});
const obs2 = new PerformanceObserver(() => {});

obs.observe({ type: 'gc' });
obs2.observe({ type: 'gc' });

0 comments on commit 013fa59

Please sign in to comment.