@@ -47,7 +47,9 @@ export default class APNSConnection {
4747 req = session . request ( headers ) ;
4848 } catch ( err ) {
4949 // Session may have been destroyed between _getSession and request
50- this . _destroySession ( ) ;
50+ if ( this . _session === session ) {
51+ this . _destroySession ( ) ;
52+ }
5153 if ( retryCount < this . _retryLimit ) {
5254 const newSession = this . _getSession ( ) ;
5355 return resolve ( this . _request ( newSession , headers , body , retryCount + 1 ) ) ;
@@ -112,7 +114,9 @@ export default class APNSConnection {
112114 log . error ( LOG_PREFIX , 'Request error: %s' , err . message ) ;
113115
114116 if ( retryCount < this . _retryLimit ) {
115- this . _destroySession ( ) ;
117+ if ( this . _session === session ) {
118+ this . _destroySession ( ) ;
119+ }
116120 const newSession = this . _getSession ( ) ;
117121 resolve ( this . _request ( newSession , headers , body , retryCount + 1 ) ) ;
118122 } else {
@@ -147,40 +151,50 @@ export default class APNSConnection {
147151 this . _connectionAttempts ++ ;
148152 this . _draining = false ;
149153
150- this . _session = http2 . connect ( this . _endpoint ) ;
154+ const session = http2 . connect ( this . _endpoint ) ;
155+ this . _session = session ;
151156
152- this . _session . on ( 'goaway' , ( ) => {
157+ session . on ( 'goaway' , ( ) => {
153158 log . warn ( LOG_PREFIX , 'Received GOAWAY from APNs, will reconnect on next request' ) ;
154- this . _draining = true ;
159+ if ( this . _session === session ) {
160+ this . _draining = true ;
161+ }
155162 } ) ;
156163
157- this . _session . on ( 'error' , ( err ) => {
164+ session . on ( 'error' , ( err ) => {
158165 log . error ( LOG_PREFIX , 'Session error: %s' , err . message ) ;
159- this . _destroySession ( ) ;
166+ if ( this . _session === session ) {
167+ this . _destroySession ( ) ;
168+ }
160169 } ) ;
161170
162- this . _session . on ( 'close' , ( ) => {
163- this . _session = null ;
164- this . _stopPing ( ) ;
171+ session . on ( 'close' , ( ) => {
172+ if ( this . _session === session ) {
173+ this . _session = null ;
174+ this . _stopPing ( ) ;
175+ }
165176 } ) ;
166177
167178 // Reset connection attempts on successful connect
168- this . _session . on ( 'connect' , ( ) => {
179+ session . on ( 'connect' , ( ) => {
169180 this . _connectionAttempts = 0 ;
170181 } ) ;
171182
172183 this . _startPing ( ) ;
173- return this . _session ;
184+ return session ;
174185 }
175186
176187 _startPing ( ) {
177188 this . _stopPing ( ) ;
178189 this . _pingInterval = setInterval ( ( ) => {
179- if ( this . _session && ! this . _session . destroyed ) {
180- this . _session . ping ( ( err ) => {
190+ const currentSession = this . _session ;
191+ if ( currentSession && ! currentSession . destroyed ) {
192+ currentSession . ping ( ( err ) => {
181193 if ( err ) {
182194 log . warn ( LOG_PREFIX , 'Ping failed: %s' , err . message ) ;
183- this . _destroySession ( ) ;
195+ if ( this . _session === currentSession ) {
196+ this . _destroySession ( ) ;
197+ }
184198 }
185199 } ) ;
186200 }
0 commit comments