Skip to content

Commit

Permalink
fix: gracefully handle when handling an error and socket is null
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleytodd committed Sep 1, 2024
1 parent 3d4f55a commit 3e9aa21
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ function finalhandler (req, res, options) {
// cannot actually respond
if (headersSent(res)) {
debug('cannot %d after headers sent', status)
req.socket.destroy()
if (req.socket) {
req.socket.destroy()
}
return
}

Expand Down
19 changes: 19 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,4 +571,23 @@ describe('finalhandler(req, res)', function () {
})
})
})

describe('req.socket', function () {
it('should not throw when socket is null', function (done) {
request(createServer(function (req, res, next) {
res.statusCode = 200;
res.end('ok')
process.nextTick(function () {
req.socket = null
next(new Error())
})
}))
.get('/')
.end(function () {
assert.strictEqual(this.res.statusCode, 200)
assert.strictEqual(this.res.text, 'ok')
done()
})
})
})
})

0 comments on commit 3e9aa21

Please sign in to comment.