Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
fix: Fix takeHeapSnapshot() truncation bug
Browse files Browse the repository at this point in the history
Fix a logic bug in `takeHeapSnapshot()` where it interpreted the item
count as the byte count, resulting in truncated snapshots.

This change hinges on the observation that the completion callback
always comes after any and all `'addHeapSnapshotChunk'` events.

I'm 95% sure after digging into the V8 inspector and its integration
with Node.js that the assumption above is correct.

If it turns out I'm mistaken, then we will most likely treat that as
a bug in Node.js, not node-inspect.

Fixes: #56
  • Loading branch information
bnoordhuis authored and Jan Krems committed Jan 24, 2018
1 parent c07adb1 commit 94f0bf9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/internal/inspect_repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -900,10 +900,8 @@ function createRepl(inspector) {
return new Promise((resolve, reject) => {
const absoluteFile = Path.resolve(filename);
const writer = FS.createWriteStream(absoluteFile);
let totalSize;
let sizeWritten = 0;
function onProgress({ done, total, finished }) {
totalSize = total;
if (finished) {
print('Heap snaphost prepared.');
} else {
Expand All @@ -913,13 +911,18 @@ function createRepl(inspector) {
function onChunk({ chunk }) {
sizeWritten += chunk.length;
writer.write(chunk);
print(`Writing snapshot: ${sizeWritten}/${totalSize}`, true);
if (sizeWritten >= totalSize) {
writer.end();
print(`Writing snapshot: ${sizeWritten}`, true);
}
function onResolve() {
writer.end(() => {
teardown();
print(`Wrote snapshot: ${absoluteFile}`);
resolve();
}
});
}
function onReject(error) {
teardown();
reject(error);
}
function teardown() {
HeapProfiler.removeListener(
Expand All @@ -932,10 +935,7 @@ function createRepl(inspector) {

print('Heap snapshot: 0/0', true);
HeapProfiler.takeHeapSnapshot({ reportProgress: true })
.then(null, (error) => {
teardown();
reject(error);
});
.then(onResolve, onReject);
});
},

Expand Down
34 changes: 34 additions & 0 deletions test/cli/heap-profiler.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';
const { test } = require('tap');
const { readFileSync, unlinkSync } = require('fs');

const startCLI = require('./start-cli');
const filename = 'node.heapsnapshot';

function cleanup() {
try {
unlinkSync(filename);
} catch (_) {
// Ignore.
}
}

cleanup();

test('Heap profiler take snapshot', (t) => {
const cli = startCLI(['examples/empty.js']);

function onFatal(error) {
cli.quit();
throw error;
}

// Check that the snapshot is valid JSON.
return cli.waitForInitialBreak()
.then(() => cli.waitForPrompt())
.then(() => cli.command('takeHeapSnapshot()'))
.then(() => JSON.parse(readFileSync(filename, 'utf8')))
.then(() => cleanup())
.then(() => cli.quit())
.then(null, onFatal);
});

0 comments on commit 94f0bf9

Please sign in to comment.