CodeGenieApp / CodeGenieApp/serverless-express

UnhandledPromiseRejection after sending response sometimes results in 502 error

Đang mở
#459 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
JavaScript
Star
5.3k
Fork
674
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu từ handler async app.get('/') và theo dõi cách serverless-express chuyển các response đã hoàn tất và các promise rejection xảy ra sau đó tới AWS Lambda và API Gateway. Tái hiện thời điểm promise2 bị reject sau đường đi của response; được xem là hoàn thành khi hành vi sau response và trạng thái client tương ứng được xác định mà không phụ thuộc vào listener unhandledRejection ở cấp process.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
aws, express, javascript, node.js
Lĩnh vực
api, backend, cloud
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.