Skip to content

Commit

Permalink
fix(ProfilingPlugin): complete after the writeStream had finished flu…
Browse files Browse the repository at this point in the history
…shing the data to the filesystem

Fixes a race-condition where `events.json` might not yet be available immediately after compilation.
  • Loading branch information
niieani committed Mar 5, 2018
1 parent ae2ae4e commit 883088e
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/debug/ProfilingPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,18 @@ class Profiler {

/**
* @param {string} outputPath The location where to write the log.
* @returns {{trace: ?, counter: number, profiler: Profiler}} The trace object
* @returns {{trace: ?, counter: number, profiler: Profiler, fsStream: WriteStream}} The trace object
*/
function createTrace(outputPath) {
const trace = new Trace({
noStream: true
});
const profiler = new Profiler(inspector);
const fsStream = fs.createWriteStream(outputPath);

let counter = 0;

trace.pipe(fs.createWriteStream(outputPath));
trace.pipe(fsStream);
// These are critical events that need to be inserted so that tools like
// chrome dev tools can load the profile.
trace.instantEvent({
Expand Down Expand Up @@ -119,7 +120,8 @@ function createTrace(outputPath) {
return {
trace,
counter,
profiler
profiler,
fsStream
};
}

Expand Down Expand Up @@ -169,16 +171,17 @@ class ProfilingPlugin {
);

// We need to write out the CPU profile when we are all done.
compiler.hooks.done.tap(
compiler.hooks.done.tapAsync(
{
name: pluginName,
stage: Infinity
},
() => {
(stats, callback) => {
tracer.profiler.stopProfiling().then(parsedResults => {
if (parsedResults === undefined) {
tracer.profiler.destroy();
tracer.trace.flush();
tracer.fsStream.end(callback);
return;
}

Expand Down Expand Up @@ -226,6 +229,7 @@ class ProfilingPlugin {

tracer.profiler.destroy();
tracer.trace.flush();
tracer.fsStream.end(callback);
});
}
);
Expand Down

0 comments on commit 883088e

Please sign in to comment.