Skip to content

Commit

Permalink
Do not double send the response if the request is destroyed but not a…
Browse files Browse the repository at this point in the history
…borted (#4963)

Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina committed Aug 9, 2023
1 parent b4b4dc7 commit c235d2d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/wrapThenable.js
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
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
})

0 comments on commit c235d2d

Please sign in to comment.