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: do not double send the response if the request is destroyed but not aborted #4963

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/wrapThenable.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ function wrapThenable (thenable, reply) {
// the request may be terminated during the reply. in this situation,
// it require an extra checking of request.aborted to see whether
// the request is killed by client.
if (payload !== undefined || (reply.sent === false && reply.raw.headersSent === false && reply.request.raw.aborted === false)) {
// Most of the times aborted will be true when destroyed is true,
// however there is a race condition where the request is not
// aborted but only destroyed.
if (payload !== undefined || (reply.sent === false && reply.raw.headersSent === false && reply.request.raw.aborted === false && reply.request.raw.destroyed === false)) {
// we use a try-catch internally to avoid adding a catch to another
// promise, increase promise perf by 10%
try {
Expand Down
22 changes: 22 additions & 0 deletions test/wrapThenable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,25 @@ test('should reject immediately when reply[kReplyHijacked] is true', t => {
const thenable = Promise.reject(new Error('Reply sent already'))
wrapThenable(thenable, reply)
})

test('should not send the payload if the raw socket was destroyed but not aborted', async t => {
const reply = {
sent: false,
raw: {
headersSent: false
},
request: {
raw: {
aborted: false,
destroyed: true
}
},
send () {
t.fail('should not send')
}
}
const thenable = Promise.resolve()
wrapThenable(thenable, reply)

await thenable
})