From 735f99c01bb82a4ea703b63d1a1ac74a8d7235b7 Mon Sep 17 00:00:00 2001 From: Sam Saccone Date: Sat, 23 Jun 2018 11:27:18 -0700 Subject: [PATCH] Fix race condition in profiling plugin. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Based on some digging in this git history for the profiling plugin it looks like this commit exacerbated the issue: 883088e Note, we are calling end() and then writeStream right after we call flush on the trace object https://github.com/samccone/chrome-trace-event/blob/64b514e9bd364ca5b6df3c0fb121ba0d3b239cc2/lib/trace-event.ts#L123 The trace object is only calling _push to the writable stream but in no way ensuring that all data that has been pushed has actually been written to the output stream sooo we find ourselves encounter 🐎 [race] conditions. --- lib/debug/ProfilingPlugin.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/debug/ProfilingPlugin.js b/lib/debug/ProfilingPlugin.js index 118d5007de3..bd6202fdbdb 100644 --- a/lib/debug/ProfilingPlugin.js +++ b/lib/debug/ProfilingPlugin.js @@ -130,7 +130,14 @@ function createTrace(outputPath) { trace, counter, profiler, - end: callback => fsStream.end(callback) + end: callback => { + // Wait until the write stream finishes. + fsStream.on("finish", () => { + callback(); + }); + // Tear down the readable trace stream. + trace.destroy(); + } }; }