From 75fd5bbb4d8cfe9b62761b38b11c1b24551ffa3c Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Thu, 4 Jul 2024 17:01:33 -0700 Subject: [PATCH] Further reduce log flooding when parallel execution terminates early --- lib/parallel_worker.js | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/parallel_worker.js b/lib/parallel_worker.js index 0a32405..ab47dec 100644 --- a/lib/parallel_worker.js +++ b/lib/parallel_worker.js @@ -26,16 +26,14 @@ class ParallelWorker { // spec files. for (const errorType of ['uncaughtException', 'unhandledRejection']) { options.process.on(errorType, error => { - if (this.clusterWorker_.isConnected()) { - this.clusterWorker_.send({ - type: errorType, - error: serializeError(error) - }); - } else { - // Don't try to report errors after disconnect. If we do, it'll cause - // another unhandled exception. The resulting error-and-reporting loop - // can keep the runner from finishing. - console.error(`${errorType} in Jasmine worker process after disconnect:`, error); + const sent = sendIfConnected(this.clusterWorker_, { + type: errorType, + error: serializeError(error) + }); + + if (!sent) { + console.error(`${errorType} in Jasmine worker process after disconnect:`, + error); console.error('This error cannot be reported properly because it ' + 'happened after the worker process was disconnected.' ); @@ -150,11 +148,7 @@ function forwardingReporter(clusterWorker) { for (const eventName of eventNames) { reporter[eventName] = function (payload) { - if (!clusterWorker.isConnected()) { - return; - } - - clusterWorker.send({ + sendIfConnected(clusterWorker, { type: 'reporterEvent', eventName, payload: { @@ -170,4 +164,22 @@ function forwardingReporter(clusterWorker) { return reporter; } +function sendIfConnected(clusterWorker, msg) { + if (clusterWorker.isConnected()) { + try { + clusterWorker.send(msg); + return true; + } catch (e) { + // EPIPE may be thrown if the worker receives a disconnect between + // the calls to isConnected() and send() above. + if (e.code !== 'EPIPE') { + throw e; + } + } + } + + return false; +} + + module.exports = ParallelWorker;