Skip to content

error in reconnecting event listener silently prevents reconnection #2234

@ryanmeador

Description

@ryanmeador

As I commented on #2032, I discovered that if the reconnecting event listener throws an error, it will silently prevent reconnection from happening. No error message will be logged, and there is no indication anything has gone wrong except that it just doesn't ever attempt to reconnect. I think it would be helpful if an error message was logged in this situation and the reconnection was able to proceed.

This is easiest to reproduce using Heroku's Redis server, which by default closes all connections after 5 minutes of inactivity.

Environment:

  • Node.js Version: 16.15.1
  • Redis Server Version: 6.2.3 (Heroku)
  • Node Redis Version: 4.2.0
  • Platform: MacOS 12.5

Reproducer:

import { createClient } from "redis";

export const client = createClient({ url: process.env.REDIS_URL });

client.on('error', err => console.log(`Redis error: ${err}`));
// the below line works fine
//client.on('reconnecting', () => console.log('Redis reconnecting'));
// the below line causes an error, which is swallowed and silently prevents reconnect
client.on('reconnecting', (foo) => console.log('Redis reconnecting' + foo.bar));
client.on('connect', () => console.log('Redis connected'));
client.on('ready', () => console.log('Redis ready'));
client.on('end', () => console.log('Redis connection closed'));

await client.connect();

Output in the broken case:

Redis connected
Redis ready
Redis error: Error: Socket closed unexpectedly

Output in the working case (uncomment the working line and comment the broken line):

Redis connected
Redis ready
Redis error: Error: Socket closed unexpectedly
Redis reconnecting
Redis connected
Redis ready

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions