Skip to content

Commit

Permalink
fix: race conditions in Redis#disconnect() can cancel reconnection …
Browse files Browse the repository at this point in the history
…unexpectedly

Closes #1138, and closes #1007
  • Loading branch information
alexandre-abrioux committed May 30, 2020
1 parent 9721ebd commit 6fad73b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/cluster/index.ts
Expand Up @@ -316,7 +316,7 @@ class Cluster extends EventEmitter {
if (!reconnect) {
this.manuallyClosing = true;
}
if (this.reconnectTimeout) {
if (this.reconnectTimeout && !reconnect) {
clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = null;
debug("Canceled reconnecting attempts");
Expand Down
9 changes: 4 additions & 5 deletions lib/redis/index.ts
Expand Up @@ -278,10 +278,9 @@ Redis.prototype.connect = function (callback) {

this.condition = {
select: options.db,
auth:
options.username
? [options.username, options.password]
: options.password,
auth: options.username
? [options.username, options.password]
: options.password,
subscriber: false,
};

Expand Down Expand Up @@ -380,7 +379,7 @@ Redis.prototype.disconnect = function (reconnect) {
if (!reconnect) {
this.manuallyClosing = true;
}
if (this.reconnectTimeout) {
if (this.reconnectTimeout && !reconnect) {
clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = null;
}
Expand Down
26 changes: 26 additions & 0 deletions test/functional/cluster/connect.ts
Expand Up @@ -384,4 +384,30 @@ describe("cluster:connect", function () {
}
});
});

describe("multiple reconnect", function () {
it("should reconnect after multiple consecutive disconnect(true) are called", function (done) {
new MockServer(30001);
const cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }], {
enableReadyCheck: false,
});
cluster.once("reconnecting", function () {
cluster.disconnect(true);
});
cluster.once("ready", function () {
cluster.disconnect(true);
const rejectTimeout = setTimeout(function () {
cluster.disconnect();
done(new Error("second disconnect(true) didn't reconnect redis"));
}, 1000);
process.nextTick(function () {
cluster.once("ready", function () {
clearTimeout(rejectTimeout);
cluster.disconnect();
done();
});
});
});
});
});
});
23 changes: 23 additions & 0 deletions test/functional/connection.ts
Expand Up @@ -451,4 +451,27 @@ describe("connection", function () {
});
});
});

describe("multiple reconnect", function () {
it("should reconnect after multiple consecutive disconnect(true) are called", function (done) {
const redis = new Redis();
redis.once("reconnecting", function () {
redis.disconnect(true);
});
redis.once("ready", function () {
redis.disconnect(true);
const rejectTimeout = setTimeout(function () {
redis.disconnect();
done(new Error("second disconnect(true) didn't reconnect redis"));
}, 1000);
process.nextTick(function () {
redis.once("ready", function () {
clearTimeout(rejectTimeout);
redis.disconnect();
done();
});
});
});
});
});
});

0 comments on commit 6fad73b

Please sign in to comment.