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

fix: Fix takeHeapSnapshot() truncation bug #58

Merged
merged 1 commit into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});