CodeGenieApp / CodeGenieApp/serverless-express

UnhandledPromiseRejection after sending response sometimes results in 502 error

オープン
#459 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
JavaScript
スター
5.3k
フォーク
676
PR マージ指標
30日以内にマージされた PR はありません

説明

### 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.

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

async app.get('/') ハンドラーから開始し、serverless-express が完了したレスポンスと後続の promise rejection を AWS Lambda と API Gateway に渡す仕組みを追跡します。レスポンスの経路の後に promise2 が reject されるタイミングを再現します。レスポンス後の動作と、その結果としてのクライアントのステータスが、プロセスレベルの unhandledRejection リスナーに依存せず定義されていれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
aws, express, javascript, node.js
領域
api, backend, cloud
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。