Bug 1295867: Wait for all IO operations to complete before ending test tasks. r?aswan
MozReview-Commit-ID: A6DIpXE5M1R
--- a/toolkit/modules/subprocess/subprocess_worker_common.js
+++ b/toolkit/modules/subprocess/subprocess_worker_common.js
@@ -57,16 +57,29 @@ class BaseProcess {
this.pipes = [];
this.stringArrays = [];
this.spawn(options);
}
/**
+ * Waits for the process to exit and all of its pending IO operations to
+ * complete.
+ *
+ * @returns {Promise<void>}
+ */
+ awaitFinished() {
+ return Promise.all([
+ this.exitPromise,
+ ...this.pipes.map(pipe => pipe.closedPromise),
+ ]);
+ }
+
+ /**
* Creates a null-terminated array of pointers to null-terminated C-strings,
* and returns it.
*
* @param {string[]} strings
* The strings to convert into a C string array.
*
* @returns {ctypes.char.ptr.array}
*/
@@ -125,18 +138,21 @@ let requests = {
return {data: {}};
},
wait(processId) {
let process = io.getProcess(processId);
process.wait();
+ process.awaitFinished().then(() => {
+ io.cleanupProcess(process);
+ });
+
return process.exitPromise.then(exitCode => {
- io.cleanupProcess(process);
return {data: {exitCode}};
});
},
read(pipeId, count) {
let pipe = io.getPipe(pipeId);
return pipe.read(count).then(buffer => {
@@ -158,17 +174,18 @@ let requests = {
getProcesses() {
let data = new Map(Array.from(io.processes.values(),
proc => [proc.id, proc.pid]));
return {data};
},
waitForNoProcesses() {
- return Promise.all(Array.from(io.processes.values(), proc => proc.exitPromise));
+ return Promise.all(Array.from(io.processes.values(),
+ proc => proc.awaitFinished()));
},
};
onmessage = event => {
io.messageCount--;
let {msg, msgId, args} = event.data;