ERR_HTTP_SOCKET_ASSIGNED
Node.jsERRORNotableHTTPHIGH confidence

HTTP socket already assigned to another response

Production Risk

Low — only affects code that manually manages HTTP sockets.

What this means

Thrown when an attempt is made to assign a socket to an HTTP ServerResponse or ClientRequest that already has a socket assigned. Each HTTP message can only be associated with one socket; reassigning the socket is not permitted.

Why it happens
  1. 1Manually calling response.assignSocket() more than once on the same response
  2. 2Custom socket management code that attempts to reassign sockets
How to reproduce

Triggered when assignSocket() is called on an HTTP message that already has an assigned socket.

trigger — this will error
trigger — this will error
const http = require('http');
http.createServer((req, res) => {
  const sock = res.socket;
  res.assignSocket(sock); // already assigned at this point
  res.end('ok');
}).listen(3000);

expected output

Error [ERR_HTTP_SOCKET_ASSIGNED]: Socket is already assigned

Fix

Check res.socket before calling assignSocket()

WHEN In custom socket-management code

Check res.socket before calling assignSocket()
if (!res.socket) {
  res.assignSocket(mySocket);
}

Why this works

Guarding on the existing socket property prevents duplicate assignment.

Code examples
Triggerjs
const http = require('http');
http.createServer((req, res) => {
  const sock = res.socket;
  res.assignSocket(sock); // already assigned at this point
  res.end('ok');
}).listen(3000);  // this triggers ERR_HTTP_SOCKET_ASSIGNED
Handle in try/catchjs
try {
  // operation that may throw ERR_HTTP_SOCKET_ASSIGNED
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_HTTP_SOCKET_ASSIGNED') {
    console.error('ERR_HTTP_SOCKET_ASSIGNED:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_http_socket_assigned(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Call assignSocket() on a response that was created by the HTTP server

The server assigns sockets automatically; manual reassignment is unnecessary and errors.

Same error in other languages
Sources
Official documentation ↗

Node.js Error Codes Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors