Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unhandled Promise rejections in pipeline.exec [skip ci] #1466

Merged
merged 1 commit into from
Nov 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions lib/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,15 @@ Pipeline.prototype.execBuffer = deprecate(function () {
return execBuffer.apply(this, arguments);
}, "Pipeline#execBuffer: Use Pipeline#exec instead");

Pipeline.prototype.exec = function (callback: CallbackFunction) {
// NOTE: To avoid an unhandled promise rejection, this will unconditionally always return this.promise,
// which always has the rejection handled by standard-as-callback
// adding the provided rejection callback.
//
// If a different promise instance were returned, that promise would cause its own unhandled promise rejection
// errors, even if that promise unconditionally resolved to **the resolved value of** this.promise.
Pipeline.prototype.exec = function (
callback: CallbackFunction
): Promise<Array<any>> {
// Wait for the cluster to be connected, since we need nodes information before continuing
if (this.isCluster && !this.redis.slots.length) {
if (this.redis.status === "wait") this.redis.connect().catch(noop);
Expand Down Expand Up @@ -338,17 +346,19 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) {

// In cluster mode, always load scripts before running the pipeline
if (this.isCluster) {
return pMap(scripts, (script) => _this.redis.script("load", script.lua), {
pMap(scripts, (script) => _this.redis.script("load", script.lua), {
concurrency: 10,
}).then(function () {
for (let i = 0; i < scripts.length; i++) {
_this.redis._addedScriptHashes[scripts[i].sha] = true;
}
return execPipeline();
});
})
.then(function () {
for (let i = 0; i < scripts.length; i++) {
_this.redis._addedScriptHashes[scripts[i].sha] = true;
}
})
.then(execPipeline, this.reject);
return this.promise;
}

return this.redis
this.redis
.script(
"exists",
scripts.map(({ sha }) => sha)
Expand All @@ -371,8 +381,9 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) {
for (let i = 0; i < scripts.length; i++) {
_this.redis._addedScriptHashes[scripts[i].sha] = true;
}
return execPipeline();
});
})
.then(execPipeline, this.reject);
return this.promise;

function execPipeline() {
let data = "";
Expand Down