Skip to content

Calls to disconnect doesn't stop reconnect  #2010

@pixtron

Description

@pixtron

If the connection to the redis server can't be established, a call to client.disconnect() won't stop the client from trying to reconnect.

Steps to reproduce

  1. run the test script below, make sure there is nothing listening on port 65535 so the connection attempt errors with ECONNREFUSED
  2. send SIGTERM or SIGINT to the process (eg. Ctrl + C)

Expected Result
Client stops trying to reconnect and process ends.

2022-02-26T14:24:09.754Z client error connect ECONNREFUSED 127.0.0.1:65535
reconnecting
2022-02-26T14:24:09.909Z client error connect ECONNREFUSED 127.0.0.1:65535
^C
connect error connect ECONNREFUSED 127.0.0.1:65535
command error The client is closed

Actual Result
The client keeps reconnecting, the process keeps running until a second SIGTERM or SIGINT is sent (script only listens once to the event)

2022-02-26T14:24:09.754Z client error connect ECONNREFUSED 127.0.0.1:65535
reconnecting
2022-02-26T14:24:09.909Z client error connect ECONNREFUSED 127.0.0.1:65535
^C
disconnect error The client is closed
reconnecting
2022-02-26T14:24:10.113Z client error connect ECONNREFUSED 127.0.0.1:65535
reconnecting
2022-02-26T14:24:10.365Z client error connect ECONNREFUSED 127.0.0.1:65535
reconnecting
^C

Environment:

  • Node.js Version: 16.13
  • Redis Server Version: 6.2.6
  • Node Redis Version: 4.0.4
  • Platform: Ubuntu 20.04.4
const { createClient } = require('redis');

let client;

const onError = (err) => {
	console.log(new Date(), 'onError', err.message);
}

const onReconnecting = () => {
	console.log(new Date(), 'onReconnecting');
}

const handleSignal =  async () => {
	if (client) try {
		await client.disconnect();
	} catch (err) {
		console.log('disconnect error', err.message);
	}
}

(async () => {
	process.once('SIGINT', handleSignal);
	process.once('SIGTERM', handleSignal);

	client = createClient({
		socket: {
			port: 65535
		}
	});

	client.on('connect', () => console.log('connect'));
	client.on('error', err => console.log(new Date(), 'client error', err.message));
	client.on('reconnecting', () => console.log('reconnecting'));

	try {
		await client.connect();
	} catch(err) {
		console.log('connect error', err.message);
	}

	try {
		await client.incr('mykey');
		console.log(await client.get('mykey'));
	} catch(err) {
		console.log('command error', err.message);
	}
})();

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions