From b3352cfba4982dd9c6e78e7609ff2e9e92582eed Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 2 Aug 2021 21:41:19 -0700 Subject: [PATCH] debugger: prevent simultaneous heap snapshots MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/nodejs/node/issues/39555 PR-URL: https://github.com/nodejs/node/pull/39638 Reviewed-By: Gireesh Punathil Reviewed-By: James M Snell Reviewed-By: Richard Lau Reviewed-By: Juan José Arboleda Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig --- lib/internal/debugger/inspect_repl.js | 11 ++++++++++- test/sequential/test-debugger-heap-profiler.js | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/internal/debugger/inspect_repl.js b/lib/internal/debugger/inspect_repl.js index a93a8dfecf2720..401887cbda5c74 100644 --- a/lib/internal/debugger/inspect_repl.js +++ b/lib/internal/debugger/inspect_repl.js @@ -325,6 +325,7 @@ function createRepl(inspector) { const history = { control: [], debug: [] }; const watchedExpressions = []; const knownBreakpoints = []; + let heapSnapshotPromise = null; let pauseOnExceptionState = 'none'; let lastCommand; @@ -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; @@ -983,6 +990,7 @@ function createRepl(inspector) { writer.end(() => { teardown(); print(`Wrote snapshot: ${absoluteFile}`); + heapSnapshotPromise = null; resolve(); }); } @@ -1006,6 +1014,7 @@ function createRepl(inspector) { HeapProfiler.takeHeapSnapshot({ reportProgress: true }), onResolve, onReject); }); + return heapSnapshotPromise; }, get watchers() { diff --git a/test/sequential/test-debugger-heap-profiler.js b/test/sequential/test-debugger-heap-profiler.js index 6ba55e8078348f..69be07a6f9c517 100644 --- a/test/sequential/test-debugger-heap-profiler.js +++ b/test/sequential/test-debugger-heap-profiler.js @@ -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); }