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 <jasnell@gmail.com>
Fixes: #38412

PR-URL: #38414
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
  • Loading branch information
jasnell committed Apr 27, 2021
1 parent 6f296b5 commit ea01548
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 ea01548

Please sign in to comment.