Skip to content

Commit 16a9ab1

Browse files
Trottnodejs-github-bot
authored andcommitted
debugger: prevent simultaneous heap snapshots
Fixes: #39555 PR-URL: #39638 Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 97b9fa3 commit 16a9ab1

File tree

3 files changed

+14
-49
lines changed

3 files changed

+14
-49
lines changed

lib/internal/debugger/inspect_repl.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ function createRepl(inspector) {
325325
const history = { control: [], debug: [] };
326326
const watchedExpressions = [];
327327
const knownBreakpoints = [];
328+
let heapSnapshotPromise = null;
328329
let pauseOnExceptionState = 'none';
329330
let lastCommand;
330331

@@ -961,7 +962,13 @@ function createRepl(inspector) {
961962
},
962963

963964
takeHeapSnapshot(filename = 'node.heapsnapshot') {
964-
return new Promise((resolve, reject) => {
965+
if (heapSnapshotPromise) {
966+
print(
967+
'Cannot take heap snapshot because another snapshot is in progress.'
968+
);
969+
return heapSnapshotPromise;
970+
}
971+
heapSnapshotPromise = new Promise((resolve, reject) => {
965972
const absoluteFile = Path.resolve(filename);
966973
const writer = FS.createWriteStream(absoluteFile);
967974
let sizeWritten = 0;
@@ -983,6 +990,7 @@ function createRepl(inspector) {
983990
writer.end(() => {
984991
teardown();
985992
print(`Wrote snapshot: ${absoluteFile}`);
993+
heapSnapshotPromise = null;
986994
resolve();
987995
});
988996
}
@@ -1006,6 +1014,7 @@ function createRepl(inspector) {
10061014
HeapProfiler.takeHeapSnapshot({ reportProgress: true }),
10071015
onResolve, onReject);
10081016
});
1017+
return heapSnapshotPromise;
10091018
},
10101019

10111020
get watchers() {

test/known_issues/test-debugger-takeHeapSnapshot-race.js

-48
This file was deleted.

test/sequential/test-debugger-heap-profiler.js

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ const filename = 'node.heapsnapshot';
3232
.then(() => cli.waitForPrompt())
3333
.then(() => cli.command('takeHeapSnapshot()'))
3434
.then(() => JSON.parse(readFileSync(filename, 'utf8')))
35+
// Check that two simultaneous snapshots don't step all over each other.
36+
// Refs: https://github.com/nodejs/node/issues/39555
37+
.then(() => cli.command('takeHeapSnapshot(); takeHeapSnapshot()'))
38+
.then(() => JSON.parse(readFileSync(filename, 'utf8')))
3539
.then(() => cli.quit())
3640
.then(null, onFatal);
3741
}

0 commit comments

Comments
 (0)