Skip to content

Commit

Permalink
Handle aborted requests (#3651)
Browse files Browse the repository at this point in the history
  • Loading branch information
Allain55 committed Feb 28, 2022
1 parent 139556f commit b2ce3d5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/reply.js
Expand Up @@ -457,7 +457,7 @@ function sendStream (payload, res, reply) {
eos(payload, { readable: true, writable: false }, function (err) {
sourceOpen = false
if (err != null) {
if (res.headersSent) {
if (res.headersSent || reply.request.raw.aborted === true) {
if (!errorLogged) {
errorLogged = true
logStreamError(reply.log, err, res)
Expand Down
48 changes: 48 additions & 0 deletions test/stream.test.js
Expand Up @@ -694,3 +694,51 @@ test('should mark reply as sent before pumping the payload stream into response
fastify.close()
})
})

test('reply.send handles aborted requests', t => {
t.plan(2)

const spyLogger = {
level: 'error',
fatal: () => { },
error: () => {
t.fail('should not log an error')
},
warn: () => { },
info: () => { },
debug: () => { },
trace: () => { },
child: () => { return spyLogger }
}
const fastify = Fastify({
logger: spyLogger
})

fastify.get('/', (req, reply) => {
setTimeout(() => {
const stream = new Readable({
read: function () {
this.push(null)
}
})
reply.send(stream)
}, 6)
})

fastify.listen({ port: 0 }, err => {
t.error(err)
fastify.server.unref()

const port = fastify.server.address().port
const http = require('http')
const req = http.get(`http://localhost:${port}`)
.on('error', (err) => {
t.equal(err.code, 'ECONNRESET')
fastify.close()
})

setTimeout(() => {
req.abort()
}, 1)
})
})

0 comments on commit b2ce3d5

Please sign in to comment.