hapijs / hapi

The Simple, Secure Framework Developers Trust
https://hapi.dev
Other
14.63k stars 1.34k forks source link

Server crashes when a route handler times out and onPreResponse is set to return h.abandon #4326

Open quepop opened 2 years ago

quepop commented 2 years ago

Support plan

Context

What are you trying to achieve or the steps to reproduce?

I added an async wait_for function to one of my route handlers so that it exceeds the timeout value of route.timeout.server (set on every route)

Body of the onPreResponse method (set on every route):

if(request.response.statusCode == 200) return h.continue
request.raw.res.destroy()
return h.abandon

The above method works as expected under any other conditions (that lead to request.response.statusCode not being equal to 200).

What was the result you got?

My app crashed and I received the following error (I manually replaced some paths with ***):

            this.response._close();
                          ^

TypeError: this.response._close is not a function
    at Request._setResponse (/home/***/node_modules/@hapi/hapi/lib/request.js:525:27)
    at exports.execute (/home/***/node_modules/@hapi/hapi/lib/handler.js:38:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Request._lifecycle (/home/***/node_modules/@hapi/hapi/lib/request.js:371:32)
    at async Request._execute (/home/***/node_modules/@hapi/hapi/lib/request.js:281:9)

What result did you expect?

I expected the route to be abandoned and the app to not crash.

quepop commented 2 years ago

I managed to fix the issue temporarily by changing the body of the onPreResponse method to:

if(request.response.statusCode == 200) return h.continue
request.raw.res.destroy()
if(
    isNativeError(request.response)         && 
    request.response.stack              && 
    request.response.stack.includes("timeoutReply")
) return h.continue
return h.abandon