@@ -236,17 +236,25 @@ export namespace ConnectionContext {
236236 try {
237237 if ( this . connection . isOpen ( ) ) {
238238 // Close all the senders.
239- for ( const senderName of Object . keys ( this . senders ) ) {
240- await this . senders [ senderName ] . close ( ) ;
241- }
239+ await Promise . all (
240+ Object . keys ( connectionContext . senders ) . map ( ( name ) =>
241+ connectionContext . senders [ name ] ?. close ( ) . catch ( ( ) => {
242+ /* error already logged, swallow it here */
243+ } )
244+ )
245+ ) ;
242246 // Close all the receivers.
243- for ( const receiverName of Object . keys ( this . receivers ) ) {
244- await this . receivers [ receiverName ] . close ( ) ;
245- }
247+ await Promise . all (
248+ Object . keys ( connectionContext . receivers ) . map ( ( name ) =>
249+ connectionContext . receivers [ name ] ?. close ( ) . catch ( ( ) => {
250+ /* error already logged, swallow it here */
251+ } )
252+ )
253+ ) ;
246254 // Close the cbs session;
247255 await this . cbsSession . close ( ) ;
248256 // Close the management session
249- await this . managementSession ! . close ( ) ;
257+ await this . managementSession ? .close ( ) ;
250258 await this . connection . close ( ) ;
251259 this . wasConnectionCloseCalled = true ;
252260 logger . info ( "Closed the amqp connection '%s' on the client." , this . connectionId ) ;
@@ -281,73 +289,90 @@ export namespace ConnectionContext {
281289 waitForConnectionRefreshPromise = new Promise ( ( resolve ) => {
282290 waitForConnectionRefreshResolve = resolve ;
283291 } ) ;
284-
285- logger . verbose (
286- "[%s] 'disconnected' event occurred on the amqp connection." ,
287- connectionContext . connection . id
288- ) ;
289-
290- if ( context . connection && context . connection . error ) {
292+ try {
291293 logger . verbose (
292- "[%s] Accompanying error on the context.connection: %O" ,
293- connectionContext . connection . id ,
294- context . connection && context . connection . error
294+ "[%s] 'disconnected' event occurred on the amqp connection." ,
295+ connectionContext . connection . id
295296 ) ;
296- }
297- if ( context . error ) {
297+
298+ if ( context . connection && context . connection . error ) {
299+ logger . verbose (
300+ "[%s] Accompanying error on the context.connection: %O" ,
301+ connectionContext . connection . id ,
302+ context . connection && context . connection . error
303+ ) ;
304+ }
305+ if ( context . error ) {
306+ logger . verbose (
307+ "[%s] Accompanying error on the context: %O" ,
308+ connectionContext . connection . id ,
309+ context . error
310+ ) ;
311+ }
312+ const state : Readonly < {
313+ wasConnectionCloseCalled : boolean ;
314+ numSenders : number ;
315+ numReceivers : number ;
316+ } > = {
317+ wasConnectionCloseCalled : connectionContext . wasConnectionCloseCalled ,
318+ numSenders : Object . keys ( connectionContext . senders ) . length ,
319+ numReceivers : Object . keys ( connectionContext . receivers ) . length
320+ } ;
298321 logger . verbose (
299- "[%s] Accompanying error on the context : %O" ,
322+ "[%s] Closing all open senders and receivers in the state : %O" ,
300323 connectionContext . connection . id ,
301- context . error
324+ state
302325 ) ;
303- }
304- const state : Readonly < {
305- wasConnectionCloseCalled : boolean ;
306- numSenders : number ;
307- numReceivers : number ;
308- } > = {
309- wasConnectionCloseCalled : connectionContext . wasConnectionCloseCalled ,
310- numSenders : Object . keys ( connectionContext . senders ) . length ,
311- numReceivers : Object . keys ( connectionContext . receivers ) . length
312- } ;
313- logger . verbose (
314- "[%s] Closing all open senders and receivers in the state: %O" ,
315- connectionContext . connection . id ,
316- state
317- ) ;
318326
319- // Clear internal map maintained by rhea to avoid reconnecting of old links once the
320- // connection is back up.
321- connectionContext . connection . removeAllSessions ( ) ;
327+ // Clear internal map maintained by rhea to avoid reconnecting of old links once the
328+ // connection is back up.
329+ connectionContext . connection . removeAllSessions ( ) ;
322330
323- // Close the cbs session to ensure all the event handlers are released.
324- await connectionContext . cbsSession . close ( ) . catch ( ( ) => {
325- /* error already logged, swallow it here */
326- } ) ;
327- // Close the management session to ensure all the event handlers are released.
328- await connectionContext . managementSession ! . close ( ) . catch ( ( ) => {
329- /* error already logged, swallow it here */
330- } ) ;
331+ // Close the cbs session to ensure all the event handlers are released.
332+ await connectionContext . cbsSession ? .close ( ) . catch ( ( ) => {
333+ /* error already logged, swallow it here */
334+ } ) ;
335+ // Close the management session to ensure all the event handlers are released.
336+ await connectionContext . managementSession ? .close ( ) . catch ( ( ) => {
337+ /* error already logged, swallow it here */
338+ } ) ;
331339
332- // Close all senders and receivers to ensure clean up of timers & other resources.
333- if ( state . numSenders || state . numReceivers ) {
334- for ( const senderName of Object . keys ( connectionContext . senders ) ) {
335- const sender = connectionContext . senders [ senderName ] ;
336- await sender . close ( ) . catch ( ( ) => {
337- /* error already logged, swallow it here */
338- } ) ;
339- }
340- for ( const receiverName of Object . keys ( connectionContext . receivers ) ) {
341- const receiver = connectionContext . receivers [ receiverName ] ;
342- await receiver . close ( ) . catch ( ( ) => {
343- /* error already logged, swallow it here */
344- } ) ;
340+ // Close all senders and receivers to ensure clean up of timers & other resources.
341+ if ( state . numSenders || state . numReceivers ) {
342+ await Promise . all (
343+ Object . keys ( connectionContext . senders ) . map ( ( name ) =>
344+ connectionContext . senders [ name ] ?. close ( ) . catch ( ( ) => {
345+ /* error already logged, swallow it here */
346+ } )
347+ )
348+ ) ;
349+
350+ await Promise . all (
351+ Object . keys ( connectionContext . receivers ) . map ( ( name ) =>
352+ connectionContext . receivers [ name ] ?. close ( ) . catch ( ( ) => {
353+ /* error already logged, swallow it here */
354+ } )
355+ )
356+ ) ;
345357 }
358+ } catch ( err ) {
359+ logger . verbose (
360+ `[${ connectionContext . connectionId } ] An error occurred while closing the connection in 'disconnected'. %O` ,
361+ err
362+ ) ;
346363 }
347364
348- await refreshConnection ( connectionContext ) ;
349- waitForConnectionRefreshResolve ( ) ;
350- waitForConnectionRefreshPromise = undefined ;
365+ try {
366+ await refreshConnection ( connectionContext ) ;
367+ } catch ( err ) {
368+ logger . verbose (
369+ `[${ connectionContext . connectionId } ] An error occurred while refreshing the connection in 'disconnected'. %O` ,
370+ err
371+ ) ;
372+ } finally {
373+ waitForConnectionRefreshResolve ( ) ;
374+ waitForConnectionRefreshPromise = undefined ;
375+ }
351376 } ;
352377
353378 const protocolError : OnAmqpEvent = async ( context : EventContext ) => {
0 commit comments