@@ -161,6 +161,7 @@ export class Server {
161
161
> ( ) ;
162
162
private sessions = new Map < http2 . ServerHttp2Session , ChannelzSessionInfo > ( ) ;
163
163
private started = false ;
164
+ private shutdown = false ;
164
165
private options : ChannelOptions ;
165
166
private serverAddressString = 'null' ;
166
167
@@ -375,6 +376,10 @@ export class Server {
375
376
throw new Error ( 'server is already started' ) ;
376
377
}
377
378
379
+ if ( this . shutdown ) {
380
+ throw new Error ( 'bindAsync called after shutdown' ) ;
381
+ }
382
+
378
383
if ( typeof port !== 'string' ) {
379
384
throw new TypeError ( 'port must be a string' ) ;
380
385
}
@@ -485,6 +490,11 @@ export class Server {
485
490
http2Server . once ( 'error' , onError ) ;
486
491
487
492
http2Server . listen ( addr , ( ) => {
493
+ if ( this . shutdown ) {
494
+ http2Server . close ( ) ;
495
+ resolve ( new Error ( 'bindAsync failed because server is shutdown' ) ) ;
496
+ return ;
497
+ }
488
498
const boundAddress = http2Server . address ( ) ! ;
489
499
let boundSubchannelAddress : SubchannelAddress ;
490
500
if ( typeof boundAddress === 'string' ) {
@@ -583,6 +593,11 @@ export class Server {
583
593
http2Server . once ( 'error' , onError ) ;
584
594
585
595
http2Server . listen ( address , ( ) => {
596
+ if ( this . shutdown ) {
597
+ http2Server . close ( ) ;
598
+ resolve ( { port : 0 , count : 0 } ) ;
599
+ return ;
600
+ }
586
601
const boundAddress = http2Server . address ( ) as AddressInfo ;
587
602
const boundSubchannelAddress : SubchannelAddress = {
588
603
host : boundAddress . address ,
@@ -637,6 +652,12 @@ export class Server {
637
652
) => {
638
653
// We only want one resolution result. Discard all future results
639
654
resolverListener . onSuccessfulResolution = ( ) => { } ;
655
+ if ( this . shutdown ) {
656
+ deferredCallback (
657
+ new Error ( `bindAsync failed because server is shutdown` ) ,
658
+ 0
659
+ ) ;
660
+ }
640
661
if ( addressList . length === 0 ) {
641
662
deferredCallback (
642
663
new Error ( `No addresses resolved for port ${ port } ` ) ,
@@ -707,6 +728,7 @@ export class Server {
707
728
}
708
729
709
730
this . started = false ;
731
+ this . shutdown = true ;
710
732
711
733
// Always destroy any available sessions. It's possible that one or more
712
734
// tryShutdown() calls are in progress. Don't wait on them to finish.
@@ -785,6 +807,7 @@ export class Server {
785
807
786
808
// Close the server if necessary.
787
809
this . started = false ;
810
+ this . shutdown = true ;
788
811
789
812
for ( const { server : http2Server , channelzRef : ref } of this
790
813
. http2ServerList ) {
0 commit comments