Bug 1295867: Wait for all IO operations to complete before ending test tasks. r?aswan draft
authorKris Maglione <maglione.k@gmail.com>
Sat, 20 Aug 2016 15:32:42 -0700
changeset 403636 f9d270b76122486b0ef4936390b37ad99e6b38f4
parent 403635 75baf2b7deec9263e8472aa32a05aefd7a2629bc
child 528964 1752529065bc833050a7e3f157157d098147bade
push id26972
push usermaglione.k@gmail.com
push dateSat, 20 Aug 2016 22:33:10 +0000
reviewersaswan
bugs1295867
milestone51.0a1
Bug 1295867: Wait for all IO operations to complete before ending test tasks. r?aswan MozReview-Commit-ID: A6DIpXE5M1R
toolkit/modules/subprocess/subprocess_worker_common.js
--- 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;