node-redis icon indicating copy to clipboard operation
node-redis copied to clipboard

Socket closed unexpectedly after 5 seconds

Open batrdn opened this issue 1 year ago • 3 comments

Description

I'm running redis with replication, with the simple client configuration createClient({ url }). After the initial connection is successfully established, the socket is closed after 5 seconds. I guess this is because the socket connectTimeout is by default 5s, and in packages/client/lib/client/socket.ts, it'll close it based on the property.

async #createSocket(): Promise<net.Socket | tls.TLSSocket> {
    const socket = this.#socketFactory.create();

    let onTimeout;
    if (this.#connectTimeout !== undefined) {
      onTimeout = () => socket.destroy(new ConnectionTimeoutError());
      socket.once('timeout', onTimeout);
      socket.setTimeout(this.#connectTimeout);
    }
    ....
    return socket;
  }

I've already taken a look into other related issues, and I tried setting pingInterval and setting socket connectTimeout to 0, and as expected, the socket is closed regardless. What is the recommended way to keep the socket connection alive?

INFO: Redis is deployed via Helm (bitnami 20.1.4) into kubernetes cluster, with one master node and 3 replicas.

Node.js Version

20.14.0

Redis Server Version

7.4.0

Node Redis Version

4.7.0

Platform

Linux

Logs

No response

batrdn avatar Oct 18 '24 13:10 batrdn

Same

qq1012827513 avatar Dec 05 '24 01:12 qq1012827513

Same here. Have you solved this problem?

yujiniii avatar Feb 27 '25 10:02 yujiniii

The “Socket closed unexpectedly” errors occur because my Redis server has timeout set to 300 seconds. When the client is idle for 5 minutes, the server closes the connection, which triggers this error. This is expected behavior due to the idle timeout.

zhaoyii avatar Sep 26 '25 08:09 zhaoyii