Skip to content

Aborted requests handling with streams #607

@Allain55

Description

@Allain55

💬 Question here

If a request gets aborted than reply.send will log an ERR_STREAM_PREMATURE_CLOSE error if a strem was given to it. But should these kind of errors be treated as errors actually?

const {Readable} = require('stream')
const {setTimeout} = require('timers/promises')
const fastify      = require('fastify')({
	logger: true,
	disableRequestLogging: true
});

fastify.get('/', async (req, res) => {
	res.header('Content-Type', 'text')

	await setTimeout(1000)
	
	// abort the request before getting here
	
	res.send(
		new Readable({
			read() {
				this.push('whatever')
				this.push(null)
			}
		})
	)
	// alternative stream: res.send(require('fs').createReadStream('package.json'))
});

fastify.listen(8080)
	.catch(console.error)

If I cancel the ongoing request somehow (close the browser tab for example) within the delay in setTimeout then reply.send results in an ERR_STREAM_PREMATURE_CLOSE error. I also get errors if I test this example using autocannon and clinic. req.raw.aborted is true so shouldn't fastify handle aborted requests and don't throw an error in this case?

I found a comment in reply.js:

// We must not treat ERR_STREAM_PREMATURE_CLOSE as
// an error because it is created by eos, not by the stream.

But this part of the code is not called in this example. Could this be a bug?

Your Environment

  • node version: 17
  • fastify version: >=3.0.0
  • os: Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions