diff --git a/lib/pipeline.ts b/lib/pipeline.ts index e0adfa5d..b85c9e00 100644 --- a/lib/pipeline.ts +++ b/lib/pipeline.ts @@ -313,12 +313,15 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) { const script = this._shaToScript[item.args[0]]; - if (!script || this.redis._addedScriptHashes[script.sha]) { + if ( + !script || + this.redis._addedScriptHashes[script.sha] || + scripts.includes(script) + ) { continue; } scripts.push(script); - this.redis._addedScriptHashes[script.sha] = true; } const _this = this; @@ -330,7 +333,12 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) { if (this.isCluster) { return pMap(scripts, (script) => _this.redis.script("load", script.lua), { concurrency: 10, - }).then(execPipeline); + }).then(function () { + for (let i = 0; i < scripts.length; i++) { + _this.redis._addedScriptHashes[scripts[i].sha] = true; + } + return execPipeline(); + }); } return this.redis @@ -352,7 +360,12 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) { }) ); }) - .then(execPipeline); + .then(function () { + for (let i = 0; i < scripts.length; i++) { + _this.redis._addedScriptHashes[scripts[i].sha] = true; + } + return execPipeline(); + }); function execPipeline() { let data = ""; diff --git a/test/functional/pipeline.ts b/test/functional/pipeline.ts index 8363dee2..cc14ca51 100644 --- a/test/functional/pipeline.ts +++ b/test/functional/pipeline.ts @@ -320,6 +320,28 @@ describe("pipeline", function () { }); }); }); + + it("should support parallel script execution", function (done) { + const random = `${Math.random()}`; + const redis = new Redis(); + redis.defineCommand("something", { + numberOfKeys: 0, + lua: `return "${random}"`, + }); + Promise.all([ + redis.multi([["something"]]).exec(), + redis.multi([["something"]]).exec(), + ]) + .then(([[first], [second]]) => { + expect(first[0]).to.equal(null); + expect(first[1]).to.equal(random); + expect(second[0]).to.equal(null); + expect(second[1]).to.equal(random); + redis.disconnect(); + done(); + }) + .catch(done); + }); }); describe("#length", function () {