Skip to content

Commit

Permalink
fix: handle possible unhandled promise rejection with autopipelining+…
Browse files Browse the repository at this point in the history
…cluster (#1467)

Deliberately add `asCallback` on the **same** Promise instance being returned,
not on a different Promise instance that resolves to the same thing.

Avoid this error:

```javascript
process.on('unhandledRejection', (reason) => console.log('unhandledRejection', reason));
const x = Promise.reject(new Error());
x.catch((e) => console.log('caught x', e));

const causesUnhandledRejection = Promise.resolve().then(() => x);
```

Related to #1466
  • Loading branch information
TysonAndre committed Nov 23, 2021
1 parent 77ad094 commit 6ad285a
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions lib/autoPipelining.ts
Expand Up @@ -121,22 +121,25 @@ export function executeWithAutoPipelining(
// On cluster mode let's wait for slots to be available
if (client.isCluster && !client.slots.length) {
if (client.status === "wait") client.connect().catch(noop);
return new CustomPromise(function (resolve, reject) {
client.delayUntilReady((err) => {
if (err) {
reject(err);
return;
}

executeWithAutoPipelining(
client,
functionName,
commandName,
args,
callback
).then(resolve, reject);
});
});
return asCallback(
new CustomPromise(function (resolve, reject) {
client.delayUntilReady((err) => {
if (err) {
reject(err);
return;
}

executeWithAutoPipelining(
client,
functionName,
commandName,
args,
null
).then(resolve, reject);
});
}),
callback
);
}

// If we have slot information, we can improve routing by grouping slots served by the same subset of nodes
Expand Down

0 comments on commit 6ad285a

Please sign in to comment.