@@ -410,6 +410,8 @@ 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 ;
414
+
413
415
/**
414
416
* Pauses the currently running monitor, it still closes the gRPC connection
415
417
* with the underlying monitor process but it doesn't stop the message handlers
@@ -419,28 +421,41 @@ export class MonitorService extends CoreClientAware implements Disposable {
419
421
* @returns
420
422
*/
421
423
async pause ( ) : Promise < void > {
422
- return new Promise ( async ( resolve ) => {
423
- if ( ! this . duplex ) {
424
- this . logger . warn (
425
- `monitor to ${ this . port ?. address } using ${ this . port ?. protocol } already stopped`
426
- ) ;
427
- return resolve ( ) ;
428
- }
429
- await new Promise < void > ( ( closeResolve ) => {
430
- if ( ! this . duplex ) {
431
- return closeResolve ( ) ;
432
- }
433
- this . duplex . write ( new MonitorRequest ( ) . setClose ( true ) , closeResolve ) ;
434
- } ) ;
435
- this . duplex . on ( 'end' , resolve ) ;
424
+ const duplex = this . duplex ;
425
+ if ( ! duplex ) {
426
+ this . logger . warn (
427
+ `monitor to ${ this . port ?. address } using ${ this . port ?. protocol } already stopped`
428
+ ) ;
429
+ return ;
430
+ }
431
+ if ( this . closingDuplex ) {
432
+ return this . closingDuplex ;
433
+ }
434
+ const deferredClose = new Deferred < void > ( ) ;
435
+ this . closingDuplex = deferredClose . promise ;
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 ( ) ;
436
441
} ) ;
442
+ duplex . write ( new MonitorRequest ( ) . setClose ( true ) ) ;
443
+ try {
444
+ await this . closingDuplex ;
445
+ } finally {
446
+ this . closingDuplex = undefined ;
447
+ }
448
+ // Sanity check
449
+ if ( ! duplex . closed ) {
450
+ throw new Error ( 'Could not close monitor connection' ) ;
451
+ }
437
452
}
438
453
439
454
/**
440
455
* Stop the monitor currently running
441
456
*/
442
457
async stop ( ) : Promise < void > {
443
- return this . pause ( ) . finally ( this . stopMessagesHandlers . bind ( this ) ) ;
458
+ return this . pause ( ) . finally ( ( ) => this . stopMessagesHandlers ( ) ) ;
444
459
}
445
460
446
461
/**
0 commit comments