@@ -66,12 +66,13 @@ export function listenToRequestData(requestId: string): RequestListener {
6666
6767 // Start connection if not already in progress
6868 if ( ! connectionPromise ) {
69- connectionPromise = redisClient . connect ( )
69+ connectionPromise = redisClient
70+ . connect ( )
7071 . then ( ( ) => {
7172 isClientConnected = true ;
7273 connectionPromise = null ; // Clear after successful connection
7374 } )
74- . catch ( ( error ) => {
75+ . catch ( ( error : unknown ) => {
7576 connectionPromise = null ; // Clear on error to allow retry
7677 throw error ; // Re-throw to propagate error
7778 } ) ;
@@ -314,14 +315,26 @@ export function listenToRequestData(requestId: string): RequestListener {
314315 // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
315316 Object . keys ( pendingPromises ) . forEach ( ( key ) => delete pendingPromises [ key ] ) ;
316317
318+ // Wait for any pending connection attempt to complete
319+ if ( connectionPromise ) {
320+ try {
321+ await connectionPromise ;
322+ } catch {
323+ // Connection failed, but we still need to clean up state
324+ connectionPromise = null ;
325+ }
326+ }
327+
317328 // Always close THIS listener's Redis client
318329 try {
319330 if ( isClientConnected ) {
320331 await redisClient . quit ( ) ;
321- isClientConnected = false ;
322332 }
323333 } catch ( error ) {
324334 console . error ( 'Error closing Redis client:' , error ) ;
335+ } finally {
336+ isClientConnected = false ;
337+ connectionPromise = null ;
325338 }
326339 } ,
327340 } ;
0 commit comments