From d94f97d6950035818a666c08447a9d5e0ef5f8c7 Mon Sep 17 00:00:00 2001 From: Paolo Insogna Date: Wed, 28 Oct 2020 15:13:22 +0100 Subject: [PATCH] fix: Make sure script caches interval is cleared. [#1215] --- lib/cluster/index.ts | 4 ++++ lib/redis/index.ts | 6 ++++++ test/functional/cluster/connect.ts | 28 ++++++++++++++++++++++++++++ test/functional/connection.ts | 22 ++++++++++++++++++++++ 4 files changed, 60 insertions(+) diff --git a/lib/cluster/index.ts b/lib/cluster/index.ts index f7b3b4d2..b74c1a92 100644 --- a/lib/cluster/index.ts +++ b/lib/cluster/index.ts @@ -331,6 +331,7 @@ class Cluster extends EventEmitter { this.setStatus("disconnecting"); clearInterval(this._addedScriptHashesCleanInterval); + this._addedScriptHashesCleanInterval = null; if (!reconnect) { this.manuallyClosing = true; @@ -362,6 +363,9 @@ class Cluster extends EventEmitter { const status = this.status; this.setStatus("disconnecting"); + clearInterval(this._addedScriptHashesCleanInterval); + this._addedScriptHashesCleanInterval = null; + this.manuallyClosing = true; if (this.reconnectTimeout) { diff --git a/lib/redis/index.ts b/lib/redis/index.ts index 491b965f..0f7ec313 100644 --- a/lib/redis/index.ts +++ b/lib/redis/index.ts @@ -406,6 +406,7 @@ Redis.prototype.connect = function (callback) { */ Redis.prototype.disconnect = function (reconnect) { clearInterval(this._addedScriptHashesCleanInterval); + this._addedScriptHashesCleanInterval = null; if (!reconnect) { this.manuallyClosing = true; @@ -698,6 +699,11 @@ Redis.prototype.sendCommand = function (command, stream) { return command.promise; } + if (command.name === "quit") { + clearInterval(this._addedScriptHashesCleanInterval); + this._addedScriptHashesCleanInterval = null; + } + let writable = this.status === "ready" || (!stream && diff --git a/test/functional/cluster/connect.ts b/test/functional/cluster/connect.ts index bb794aad..f1e4ba33 100644 --- a/test/functional/cluster/connect.ts +++ b/test/functional/cluster/connect.ts @@ -411,3 +411,31 @@ describe("cluster:connect", function () { }); }); }); + +describe("cluster:disconnect", function () { + it("should clear the added script hashes interval when disconnecting", function (done) { + new MockServer(30001); + const cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }], { + enableReadyCheck: false, + }); + cluster.once("ready", function () { + cluster.disconnect(); + + expect(cluster._addedScriptHashesCleanInterval).to.be.null; + done(); + }); + }); + + it("should clear the added script hashes interval when quitting", function (done) { + new MockServer(30001); + const cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }], { + enableReadyCheck: false, + }); + cluster.once("ready", function () { + cluster.quit(); + + expect(cluster._addedScriptHashesCleanInterval).to.be.null; + done(); + }); + }); +}); diff --git a/test/functional/connection.ts b/test/functional/connection.ts index 34e2aac7..90064b29 100644 --- a/test/functional/connection.ts +++ b/test/functional/connection.ts @@ -475,3 +475,25 @@ describe("connection", function () { }); }); }); + +describe("disconnection", function () { + it("should clear the added script hashes interval when disconnecting", function (done) { + const redis = new Redis(); + redis.once("ready", function () { + redis.disconnect(); + + expect(redis._addedScriptHashesCleanInterval).to.be.null; + done(); + }); + }); + + it("should clear the added script hashes interval when quitting", function (done) { + const redis = new Redis(); + redis.once("ready", function () { + redis.quit(); + + expect(redis._addedScriptHashesCleanInterval).to.be.null; + done(); + }); + }); +});