denoland/deno · error · NodeError

ERR_HTTP_SOCKET_ASSIGNED

ERR_HTTP_SOCKET_ASSIGNED

Error message

ServerResponse has an already assigned socket

What it means

Thrown by ServerResponse.prototype.assignSocket when the target socket already has an _httpMessage property, i.e. it is already bound to another ServerResponse. assignSocket is the low-level hook used to attach a free socket to a response (the server does it internally for keep-alive/pipelining, and custom server or upgrade code may call it). One response owns a socket at a time; re-assigning would orphan the previous response's close handling, so it refuses with ERR_HTTP_SOCKET_ASSIGNED.

Source

Thrown at ext/node/polyfills/_http_server.js:384

      response: this,
    });
  }
}
ObjectSetPrototypeOf(ServerResponse.prototype, OutgoingMessage.prototype);
ObjectSetPrototypeOf(ServerResponse, OutgoingMessage);

ServerResponse.prototype.statusCode = 200;
ServerResponse.prototype.statusMessage = undefined;

function onServerResponseClose() {
  if (this._httpMessage) {
    emitCloseNT(this._httpMessage);
  }
}

ServerResponse.prototype.assignSocket = function assignSocket(socket) {
  if (socket._httpMessage) {
    throw new ERR_HTTP_SOCKET_ASSIGNED();
  }
  socket._httpMessage = this;
  if (socket._parent) {
    socket._parent._httpMessage = this;
    socket._parent._httpMessageDetached = false;
  }
  socket.on("close", onServerResponseClose);
  this.socket = socket;
  this.emit("socket", socket);
  this._flush();
};

ServerResponse.prototype.detachSocket = function detachSocket(socket) {
  assert(socket._httpMessage === this);
  socket.removeListener("close", onServerResponseClose);
  socket._httpMessage = null;
  socket._httpMessageDetached = true;
  if (socket._parent) {

View on GitHub (pinned to 89f33cbef2)

Solutions

  1. Do not call assignSocket on an already-attached socket; use a newly accepted or fully detached socket
  2. Check socket._httpMessage first and skip (or wait for its 'close'/'free' event) when it is set
  3. If you took over a socket (e.g. after upgrade), detach the previous response / clear socket._httpMessage before re-assigning
  4. Prefer the public server API (server.on('connection'), res.socket) over manual socket assignment

Example fix

// before
res.assignSocket(clientSocket); // throws ERR_HTTP_SOCKET_ASSIGNED if already bound

// after
if (!clientSocket._httpMessage && !clientSocket.destroyed) {
  res.assignSocket(clientSocket);
}
Defensive patterns

Strategy: validation

Validate before calling

function socketIsAssignable(socket) {
  return !!socket && !socket._httpMessage && !socket.destroyed;
}
// before attaching
if (socketIsAssignable(sock)) {
  res.assignSocket(sock);
}

Try / catch

try {
  res.assignSocket(sock);
} catch (e) {
  if (e.code === 'ERR_HTTP_SOCKET_ASSIGNED') {
    // socket already owned by another response: pick a different socket or detach first
  } else throw e;
}

Prevention

When it happens

Trigger: Calling res.assignSocket(socket) on a socket previously assigned and never detached: calling assignSocket twice on the same socket, manually re-attaching a client socket for custom keep-alive handling, or assigning a socket the server already picked for a pipelined response.

Common situations: Custom HTTP servers or proxies built on http.ServerResponse internals; code copied from Node internals that manages its own socket pool; CONNECT/upgrade handling that re-attaches a socket the server already claimed.

Related errors


AI-assisted analysis of denoland/deno@89f33cbef2 (2026-08-16). Data as JSON: /api/errors/f32d445f9a15e0d0. Report an issue: GitHub.