Skip to content

Commit 19ebed9

Browse files
authored
Merge pull request webpack#6678 from niieani/profiling-race
fix(ProfilingPlugin): complete after the writeStream had finished flushing the data to the filesystem
2 parents 6ddba9b + bd043f8 commit 19ebed9

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lib/debug/ProfilingPlugin.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,18 @@ class Profiler {
7373

7474
/**
7575
* @param {string} outputPath The location where to write the log.
76-
* @returns {{trace: ?, counter: number, profiler: Profiler}} The trace object
76+
* @returns {{trace: ?, counter: number, profiler: Profiler, end: Function}} The trace object
7777
*/
7878
function createTrace(outputPath) {
7979
const trace = new Trace({
8080
noStream: true
8181
});
8282
const profiler = new Profiler(inspector);
83+
const fsStream = fs.createWriteStream(outputPath);
8384

8485
let counter = 0;
8586

86-
trace.pipe(fs.createWriteStream(outputPath));
87+
trace.pipe(fsStream);
8788
// These are critical events that need to be inserted so that tools like
8889
// chrome dev tools can load the profile.
8990
trace.instantEvent({
@@ -119,7 +120,8 @@ function createTrace(outputPath) {
119120
return {
120121
trace,
121122
counter,
122-
profiler
123+
profiler,
124+
end: callback => fsStream.end(callback)
123125
};
124126
}
125127

@@ -169,16 +171,17 @@ class ProfilingPlugin {
169171
);
170172

171173
// We need to write out the CPU profile when we are all done.
172-
compiler.hooks.done.tap(
174+
compiler.hooks.done.tapAsync(
173175
{
174176
name: pluginName,
175177
stage: Infinity
176178
},
177-
() => {
179+
(stats, callback) => {
178180
tracer.profiler.stopProfiling().then(parsedResults => {
179181
if (parsedResults === undefined) {
180182
tracer.profiler.destroy();
181183
tracer.trace.flush();
184+
tracer.end(callback);
182185
return;
183186
}
184187

@@ -226,6 +229,7 @@ class ProfilingPlugin {
226229

227230
tracer.profiler.destroy();
228231
tracer.trace.flush();
232+
tracer.end(callback);
229233
});
230234
}
231235
);

0 commit comments

Comments
 (0)