@@ -590,35 +590,42 @@ export function _runTaskList(
590
590
591
591
const promise = defer ( ) ;
592
592
593
- async function deactivate ( ) {
594
- if ( workerPool . _active ) {
595
- workerPool . _active = false ;
596
- const errors : Error [ ] = [ ] ;
597
- try {
598
- await localQueue ?. release ( ) ;
599
- } catch ( rawE ) {
600
- const e = coerceError ( rawE ) ;
601
- errors . push ( e ) ;
602
- // Log but continue regardless
603
- logger . error ( `Releasing local queue failed: ${ e } ` , { error : rawE } ) ;
604
- }
605
- try {
606
- // Note: this runs regardless of success of the above
607
- await onDeactivate ?.( ) ;
608
- } catch ( rawE ) {
609
- const e = coerceError ( rawE ) ;
610
- errors . push ( e ) ;
611
- // Log but continue regardless
612
- logger . error ( `onDeactivate raised an error: ${ e } ` , { error : rawE } ) ;
613
- }
593
+ let deactivatePromise : Promise < void > | null = null ;
614
594
615
- if ( errors . length > 0 ) {
616
- throw new AggregateError (
617
- errors ,
618
- "Errors occurred whilst deactivating queue" ,
619
- ) ;
620
- }
595
+ function deactivate ( ) {
596
+ if ( ! deactivatePromise ) {
597
+ deactivatePromise = ( async ( ) => {
598
+ if ( workerPool . _active ) {
599
+ workerPool . _active = false ;
600
+ const errors : Error [ ] = [ ] ;
601
+ try {
602
+ await localQueue ?. release ( ) ;
603
+ } catch ( rawE ) {
604
+ const e = coerceError ( rawE ) ;
605
+ errors . push ( e ) ;
606
+ // Log but continue regardless
607
+ logger . error ( `Releasing local queue failed: ${ e } ` , { error : rawE } ) ;
608
+ }
609
+ try {
610
+ // Note: this runs regardless of success of the above
611
+ await onDeactivate ?.( ) ;
612
+ } catch ( rawE ) {
613
+ const e = coerceError ( rawE ) ;
614
+ errors . push ( e ) ;
615
+ // Log but continue regardless
616
+ logger . error ( `onDeactivate raised an error: ${ e } ` , { error : rawE } ) ;
617
+ }
618
+
619
+ if ( errors . length > 0 ) {
620
+ throw new AggregateError (
621
+ errors ,
622
+ "Errors occurred whilst deactivating queue" ,
623
+ ) ;
624
+ }
625
+ }
626
+ } ) ( ) ;
621
627
}
628
+ return deactivatePromise ;
622
629
}
623
630
624
631
let terminated = false ;
0 commit comments