Skip to content

upgrade socket & clientError #20477

@ronag

Description

@ronag

When handling a on('request', (req, res) => {}) on does not have to add listener for the error events on req and res since any errors will be forwarded to clientError.

However, when doing on('upgrade', (req, socket, head) => {}), socket can emit an error which might be uncaugth. On common way to implement upgrade is:

server.on(`upgrade`, (req, socket, head) => {
   handle(req, socket, head, err => {
    if (err) {
      // socket.on('error', () => {}) Should this really be neccessary?
      if (socket.writable && err.statusCode) {
        socket.end(Buffer.from(`HTTP/1.1 ${err.statusCode} ${getStatusText(err.statusCode)}\r\n\r\n`, 'ascii'))
      } else {
        socket.destroy()
      }
    }
  })
})

However, the problem with the above is that it might crash the server if the callback is called with an error and either socket.end or socket.destroy is called and an error is emitted afterward. In such a case shouldn't forward the error to clientError automatically for the user?

Metadata

Metadata

Assignees

No one assigned

    Labels

    httpIssues or PRs related to the http subsystem.questionIssues that look for answers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions