@@ -26,16 +26,14 @@ class ParallelWorker {
26
26
// spec files.
27
27
for ( const errorType of [ 'uncaughtException' , 'unhandledRejection' ] ) {
28
28
options . process . on ( errorType , error => {
29
- if ( this . clusterWorker_ . isConnected ( ) ) {
30
- this . clusterWorker_ . send ( {
31
- type : errorType ,
32
- error : serializeError ( error )
33
- } ) ;
34
- } else {
35
- // Don't try to report errors after disconnect. If we do, it'll cause
36
- // another unhandled exception. The resulting error-and-reporting loop
37
- // can keep the runner from finishing.
38
- console . error ( `${ errorType } in Jasmine worker process after disconnect:` , error ) ;
29
+ const sent = sendIfConnected ( this . clusterWorker_ , {
30
+ type : errorType ,
31
+ error : serializeError ( error )
32
+ } ) ;
33
+
34
+ if ( ! sent ) {
35
+ console . error ( `${ errorType } in Jasmine worker process after disconnect:` ,
36
+ error ) ;
39
37
console . error ( 'This error cannot be reported properly because it ' +
40
38
'happened after the worker process was disconnected.'
41
39
) ;
@@ -150,11 +148,7 @@ function forwardingReporter(clusterWorker) {
150
148
151
149
for ( const eventName of eventNames ) {
152
150
reporter [ eventName ] = function ( payload ) {
153
- if ( ! clusterWorker . isConnected ( ) ) {
154
- return ;
155
- }
156
-
157
- clusterWorker . send ( {
151
+ sendIfConnected ( clusterWorker , {
158
152
type : 'reporterEvent' ,
159
153
eventName,
160
154
payload : {
@@ -170,4 +164,22 @@ function forwardingReporter(clusterWorker) {
170
164
return reporter ;
171
165
}
172
166
167
+ function sendIfConnected ( clusterWorker , msg ) {
168
+ if ( clusterWorker . isConnected ( ) ) {
169
+ try {
170
+ clusterWorker . send ( msg ) ;
171
+ return true ;
172
+ } catch ( e ) {
173
+ // EPIPE may be thrown if the worker receives a disconnect between
174
+ // the calls to isConnected() and send() above.
175
+ if ( e . code !== 'EPIPE' ) {
176
+ throw e ;
177
+ }
178
+ }
179
+ }
180
+
181
+ return false ;
182
+ }
183
+
184
+
173
185
module . exports = ParallelWorker ;
0 commit comments