Skip to content

Commit

Permalink
debugger: prevent simultaneous heap snapshots
Browse files Browse the repository at this point in the history
Fixes: #39555

PR-URL: #39638
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
Trott authored and targos committed Sep 4, 2021
1 parent a45bf2f commit b3352cf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/internal/debugger/inspect_repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ function createRepl(inspector) {
const history = { control: [], debug: [] };
const watchedExpressions = [];
const knownBreakpoints = [];
let heapSnapshotPromise = null;
let pauseOnExceptionState = 'none';
let lastCommand;

Expand Down Expand Up @@ -961,7 +962,13 @@ function createRepl(inspector) {
},

takeHeapSnapshot(filename = 'node.heapsnapshot') {
return new Promise((resolve, reject) => {
if (heapSnapshotPromise) {
print(
'Cannot take heap snapshot because another snapshot is in progress.'
);
return heapSnapshotPromise;
}
heapSnapshotPromise = new Promise((resolve, reject) => {
const absoluteFile = Path.resolve(filename);
const writer = FS.createWriteStream(absoluteFile);
let sizeWritten = 0;
Expand All @@ -983,6 +990,7 @@ function createRepl(inspector) {
writer.end(() => {
teardown();
print(`Wrote snapshot: ${absoluteFile}`);
heapSnapshotPromise = null;
resolve();
});
}
Expand All @@ -1006,6 +1014,7 @@ function createRepl(inspector) {
HeapProfiler.takeHeapSnapshot({ reportProgress: true }),
onResolve, onReject);
});
return heapSnapshotPromise;
},

get watchers() {
Expand Down
4 changes: 4 additions & 0 deletions test/sequential/test-debugger-heap-profiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const filename = 'node.heapsnapshot';
.then(() => cli.waitForPrompt())
.then(() => cli.command('takeHeapSnapshot()'))
.then(() => JSON.parse(readFileSync(filename, 'utf8')))
// Check that two simultaneous snapshots don't step all over each other.
// Refs: https://github.com/nodejs/node/issues/39555
.then(() => cli.command('takeHeapSnapshot(); takeHeapSnapshot()'))
.then(() => JSON.parse(readFileSync(filename, 'utf8')))
.then(() => cli.quit())
.then(null, onFatal);
}

0 comments on commit b3352cf

Please sign in to comment.