hapi icon indicating copy to clipboard operation
hapi copied to clipboard

Error: Request timeout from node http

Open radoslavirha opened this issue 2 years ago • 3 comments

Support plan

  • is this issue currently blocking your project? yes:
  • is this issue affecting a production system? yes:

Context

  • node version: 18
  • module version with issue: 21.2.0
  • last module version without issue: not known
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): hapi application
  • any other relevant information:

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

I'm experiencing Bad Request responses when uploading large zip archives on slow internet connection (My colleagues have slower internet connection + archive size varies so I throttle requests in chrome).

Basically I just createWriteStream and pipe payload to writer in handler.

My route:

const route = {
  method: 'POST',
  path: '/import/archive',
  handler: Handler.importArchive,
  options: {
    payload: {
      allow: [ 'application/zip', 'application/x-zip-compressed' ],
      maxBytes: Bytes('15gb'),
      output: 'stream',
      parse: false
      timeout: false
    },
    timeout: {
      server: false,
      socket: false
    }
  }

But timeouts does not work and it always crashes after ~5 mins

image

I think issue is somewhere in https://github.com/hapijs/hapi/blob/71ffe08d5a4d7becafee93d5e2fe1879e5bab520/lib/core.js#L543

where listener has default requestTimeout = 300000 - it is 5mins reported by browser ^^

Node.js 18 requestTimeout docs

What was the result you got?

{
  timestamp: 1681322109764,
  tags: [ 'connection', 'client', 'error' ],
  error: Error: Request timeout
      at new NodeError (node:internal/errors:400:5)
      at onRequestTimeout (node:_http_server:780:30)
      at Server.checkConnections (node:_http_server:593:7)
      at listOnTimeout (node:internal/timers:564:17)
      at processTimers (node:internal/timers:507:7) {
    code: 'ERR_HTTP_REQUEST_TIMEOUT'
  },
  channel: 'internal'
}

from

server.events.on('log', ...)

And

Error: Bad Request
    at Server.<anonymous> (node_modules/@hapi/hapi/lib/core.js:554:40)
    at Server.emit (node:events:513:28)
    at Socket.socketOnError (node:_http_server:818:20)
    at onRequestTimeout (node:_http_server:780:17)
    at Server.checkConnections (node:_http_server:593:7)
    at listOnTimeout (node:internal/timers:564:17)
    at processTimers (node:internal/timers:507:7) {
  data: null,
  isBoom: true,
  isServer: false,
  output: {
    statusCode: 400,
    payload: { statusCode: 400, error: 'Bad Request', message: 'Bad Request' },
    headers: { connection: 'close' }
  }
}

from

server.ext('onPreResponse', (request, h) => {
    console.log(request.response);
    return h.continue;
  })

What result did you expect?

Successfully import archives

Is it possible to solve it? I need to increase timeout only for 1-2 routes so I wondered it could be solved with options.timeout.server/socket or options.payload.timeout.

Thanks

radoslavirha avatar Apr 12 '23 18:04 radoslavirha

Could this be possibly caused by the missing comma after the parse option?

michaellasky avatar Apr 28 '23 03:04 michaellasky

Hi @michaellasky ^^ is just an example with missing comma. I use eslint in code and i double checked the comma is there.

radoslavirha avatar Apr 29 '23 14:04 radoslavirha

Hi,

I got the same issue and it seems related to https://github.com/hapijs/hapi/issues/4447 Setting server.listener.requestTimeout = 0 has solved the issue in my case :)

Best, Adrien

Bacto avatar Jul 25 '23 08:07 Bacto