Skip to content

fix: premature request.signal abort on POST#6725

Open
bausmeier wants to merge 5 commits into
fastify:mainfrom
decode-development:fix/request-signal-post-abort
Open

fix: premature request.signal abort on POST#6725
bausmeier wants to merge 5 commits into
fastify:mainfrom
decode-development:fix/request-signal-post-abort

Conversation

@bausmeier
Copy link
Copy Markdown

Summary

I ran into a bug when trying to use the handlerTimeout option introduced in #6521.

I managed to narrow it down to only requests with bodies, and did some debugging to identify the root cause from there.

What it boils down to is that the 'close' event on the raw IncomingMessage fires both when the request body stream is fully consumed and when the underlying connection is aborted. The handlerTimeout abort listeners treated every close as a disconnect, so POST requests with a body had their request.signal aborted as soon as the body finished streaming, and before the handler had a chance to use it.

This fix guards the onAbort listeners with an aborted check on the IncomingMessage so the signal only aborts when the client genuinely disconnects, leaving normal body completion alone.

Checklist

bausmeier added 2 commits May 14, 2026 19:23
In preparation for adding a test which passes a signal to the timeout.
The 'close' event on the raw IncomingMessage fires both when the request
body stream is fully consumed and when the underlying connection is
aborted. The handlerTimeout abort listeners treated every close as a
disconnect, so POST requests with a body had their request.signal
aborted as soon as the body finished streaming, and before the handler
had a chance to use it.

Guard both onAbort listeners with an aborted check on the
IncomingMessage so the signal only aborts when the client genuinely
disconnects, leaving normal body completion alone.
Comment thread test/handler-timeout.test.js Outdated
Comment thread test/handler-timeout.test.js Outdated
Avoid shadowing the global setTimeout.
@bausmeier bausmeier changed the title Fix premature request.signal abort on POST fix: premature request.signal abort on POST May 15, 2026
bausmeier added 2 commits May 15, 2026 08:49
Make it very clear that the abort is not a result of the timeout.
The onAbort handler unconditionally cleared the handler timeout timer
whenever the underlying request emitted 'close', including the normal
end-of-body close on POST requests. This caused the handler timeout to
be cancelled before the slow handler had a chance to time out, so
clients received no FST_ERR_HANDLER_TIMEOUT response.

Only clear the timer when the request was actually aborted, and add a
regression test covering a slow POST handler with a request body.
@bausmeier
Copy link
Copy Markdown
Author

I found another scenario which my fix hadn't catered for. I've now fixed that too and added an additional test.

@bausmeier bausmeier requested a review from mcollina May 15, 2026 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants