CodeGenieApp / CodeGenieApp/serverless-express
UnhandledPromiseRejection after sending response sometimes results in 502 error
- Langage dominant
- JavaScript
- Étoiles
- 5.3k
- Forks
- 676
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Description
### Description
I think there exists a timing problem between sending the HTTP response passed to express and promise rejections happening afterwards.
See this pseudo code:
```
app.get('/', async (req, res) => {
const promise1 = Promise.reject({promise: 1});
const promise2 = Promise.reject({promise: 2});
let result;
try {
result = await promise1;
} catch (e) {
console.error(e);
return res.status(400).end();
}
try {
return res
.status(200)
.json(await promise2)
.end();
} catch (e) {
console.error(e);
return res.status(400).end();
}
}
);
```
I have an AWS Lambda behind an AWS API Gateway with a similar structure (my promises fetch HTTP resources).
When the request handler returns before the second promise is handled there will be an `UnhandledPromiseRejection`. Apparently `UnhandledPromiseRejection`s terminate the AWS Lambda execution (see https://forums.aws.amazon.com/message.jspa?messageID=906365).
On client side I sometimes get a 400 (--> my response) and sometimes a 502 (--> AWS response) status code.
With the workaround mentioned in the link above I always get the desired 400 response.
```
process.removeAllListeners('unhandledRejection');
process.on('unhandledRejection', (err) => console.error(err));
```
I don't know if the workaround is a good way to handle this situation but I think one solution or another should be part of this library. Of cause `UnhandledPromiseRejection`s should only be caught after a HTTP response was passed to express.
Guide de contribution
Ouvrir le guide de contribution
Piste de recherche
Commencez par le gestionnaire async app.get('/') et suivez la manière dont serverless-express transmet les réponses terminées et les rejets de promesses ultérieurs à AWS Lambda et API Gateway. Reproduisez le moment où promise2 est rejetée après le chemin de réponse ; c’est terminé lorsque le comportement après la réponse et le statut client qui en résulte sont définis sans dépendre d’un listener unhandledRejection au niveau du processus.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- aws, express, javascript, node.js
- Domaine
- api, backend, cloud
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- À clarifier
- Accessibilité débutants
- 25/100