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 nodejs-github-bot committed Aug 5, 2021
1 parent 97b9fa3 commit 16a9ab1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 49 deletions.
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
48 changes: 0 additions & 48 deletions test/known_issues/test-debugger-takeHeapSnapshot-race.js

This file was deleted.

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 16a9ab1

Please sign in to comment.