From 6439c2033d3eedf0ba8da39e3fdf1bfad833fdda Mon Sep 17 00:00:00 2001 From: Marc Bachmann Date: Mon, 31 Jan 2022 01:06:57 +0100 Subject: [PATCH] fix: Reset loaded script hashes to force a reload of scripts after reconnect of redis --- lib/redis/index.ts | 4 ++++ test/functional/pipeline.ts | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/redis/index.ts b/lib/redis/index.ts index ac3dd1d3..2cefec5d 100644 --- a/lib/redis/index.ts +++ b/lib/redis/index.ts @@ -316,6 +316,10 @@ Redis.prototype.connect = function (callback) { // Make sure only one timer is active at a time clearInterval(this._addedScriptHashesCleanInterval); + // Scripts need to get reset on reconnect as redis + // might have been restarted or some failover happened + this._addedScriptHashes = {}; + // Start the script cache cleaning this._addedScriptHashesCleanInterval = setInterval(() => { this._addedScriptHashes = {}; diff --git a/test/functional/pipeline.ts b/test/functional/pipeline.ts index ff5a854f..cd0228cb 100644 --- a/test/functional/pipeline.ts +++ b/test/functional/pipeline.ts @@ -353,6 +353,44 @@ describe("pipeline", function () { }) .catch(done); }); + + it("should reload scripts on redis restart (reconnect)", async function () { + const redis = new Redis({ connectionName: "load-script-on-reconnect" }); + const redis2 = new Redis(); + redis.defineCommand("exeecafterreconnect", { + numberOfKeys: 0, + lua: `return "OK"`, + }); + + const [[err, res]] = await redis.multi([["exeecafterreconnect"]]).exec(); + expect(err).to.equal(null); + expect(res).to.equal("OK"); + + const client = await redis.client("list").then((clients) => { + const myInfo = clients + .split("\n") + .find((client) => client.includes("load-script-on-reconnect")); + + const match = / addr=([^ ]+)/.exec(myInfo); + if (match) return match[1]; + }); + + await redis2.script("flush"); + await redis2.client("kill", "addr", client); + + // Wait for reconnect, at the moment scripts are not loaded + // if the pipeline starts before ioredis reconnects + await redis.ping(); + + const [[err2, res2]] = await redis + .multi([["exeecafterreconnect"]]) + .exec(); + + expect(err2).to.equal(null); + expect(res2).to.equal("OK"); + redis.disconnect(); + redis2.disconnect(); + }); }); describe("#length", function () {