Skip to content

Commit f357a31

Browse files
authoredFeb 6, 2022
fix: Reset loaded script hashes to force a reload of scripts after reconnect of redis (#1497)
1 parent c72f242 commit f357a31

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
 

‎lib/redis/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ Redis.prototype.connect = function (callback) {
323323
// Make sure only one timer is active at a time
324324
this.clearAddedScriptHashesCleanInterval();
325325

326+
// Scripts need to get reset on reconnect as redis
327+
// might have been restarted or some failover happened
328+
this._addedScriptHashes = {};
329+
326330
// Start the script cache cleaning
327331
this._addedScriptHashesCleanInterval = setInterval(() => {
328332
this._addedScriptHashes = {};

‎test/functional/pipeline.ts

+38
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,44 @@ describe("pipeline", function () {
353353
})
354354
.catch(done);
355355
});
356+
357+
it("should reload scripts on redis restart (reconnect)", async function () {
358+
const redis = new Redis({ connectionName: "load-script-on-reconnect" });
359+
const redis2 = new Redis();
360+
redis.defineCommand("exeecafterreconnect", {
361+
numberOfKeys: 0,
362+
lua: `return "OK"`,
363+
});
364+
365+
const [[err, res]] = await redis.multi([["exeecafterreconnect"]]).exec();
366+
expect(err).to.equal(null);
367+
expect(res).to.equal("OK");
368+
369+
const client = await redis.client("list").then((clients) => {
370+
const myInfo = clients
371+
.split("\n")
372+
.find((client) => client.includes("load-script-on-reconnect"));
373+
374+
const match = / addr=([^ ]+)/.exec(myInfo);
375+
if (match) return match[1];
376+
});
377+
378+
await redis2.script("flush");
379+
await redis2.client("kill", "addr", client);
380+
381+
// Wait for reconnect, at the moment scripts are not loaded
382+
// if the pipeline starts before ioredis reconnects
383+
await redis.ping();
384+
385+
const [[err2, res2]] = await redis
386+
.multi([["exeecafterreconnect"]])
387+
.exec();
388+
389+
expect(err2).to.equal(null);
390+
expect(res2).to.equal("OK");
391+
redis.disconnect();
392+
redis2.disconnect();
393+
});
356394
});
357395

358396
describe("#length", function () {

0 commit comments

Comments
 (0)
Please sign in to comment.