@@ -410,7 +410,7 @@ export class MonitorService extends CoreClientAware implements Disposable {
410
410
] ) as Promise < unknown > as Promise < void > ;
411
411
}
412
412
413
- private closingDuplex : Promise < void > | undefined ;
413
+ private endingDuplex : Promise < void > | undefined ;
414
414
415
415
/**
416
416
* Pauses the currently running monitor, it still closes the gRPC connection
@@ -428,26 +428,28 @@ export class MonitorService extends CoreClientAware implements Disposable {
428
428
) ;
429
429
return ;
430
430
}
431
- if ( this . closingDuplex ) {
432
- return this . closingDuplex ;
431
+ if ( this . endingDuplex ) {
432
+ return this . endingDuplex ;
433
433
}
434
- const deferredClose = new Deferred < void > ( ) ;
435
- this . closingDuplex = deferredClose . promise ;
434
+ const deferredEnd = new Deferred < void > ( ) ;
435
+ this . endingDuplex = deferredEnd . promise ;
436
436
437
- // to terminate the monitor connection, send a close request, and wait for the expected cancellation error
438
- duplex . once ( 'error' , ( error ) => {
439
- // TODO: check if cancel error
440
- deferredClose . resolve ( ) ;
437
+ // to terminate the monitor connection, send a close request, and wait for the end event
438
+ duplex . once ( 'end' , ( ) => {
439
+ deferredEnd . resolve ( ) ;
441
440
} ) ;
442
- duplex . write ( new MonitorRequest ( ) . setClose ( true ) ) ;
443
441
try {
444
- await this . closingDuplex ;
442
+ await new Promise ( ( resolve ) =>
443
+ duplex . write ( new MonitorRequest ( ) . setClose ( true ) , resolve )
444
+ ) ;
445
+ await this . endingDuplex ;
445
446
} finally {
446
- this . closingDuplex = undefined ;
447
+ this . endingDuplex = undefined ;
447
448
}
448
449
// Sanity check
449
- if ( ! duplex . closed ) {
450
- throw new Error ( 'Could not close monitor connection' ) ;
450
+ // Duplexes are allowed to be half open, check whether the monitor server (the readable) has ended
451
+ if ( ! duplex . readableEnded ) {
452
+ throw new Error ( 'Could not end the monitor connection' ) ;
451
453
}
452
454
}
453
455
0 commit comments