{"record":{"id":"f32d445f9a15e0d0","repo":"denoland/deno","slug":"err-http-socket-assigned","errorCode":"ERR_HTTP_SOCKET_ASSIGNED","errorMessage":"ServerResponse has an already assigned socket","messagePattern":"ServerResponse has an already assigned socket","errorType":"exception","errorClass":"NodeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/_http_server.js","lineNumber":384,"sourceCode":"      response: this,\n    });\n  }\n}\nObjectSetPrototypeOf(ServerResponse.prototype, OutgoingMessage.prototype);\nObjectSetPrototypeOf(ServerResponse, OutgoingMessage);\n\nServerResponse.prototype.statusCode = 200;\nServerResponse.prototype.statusMessage = undefined;\n\nfunction onServerResponseClose() {\n  if (this._httpMessage) {\n    emitCloseNT(this._httpMessage);\n  }\n}\n\nServerResponse.prototype.assignSocket = function assignSocket(socket) {\n  if (socket._httpMessage) {\n    throw new ERR_HTTP_SOCKET_ASSIGNED();\n  }\n  socket._httpMessage = this;\n  if (socket._parent) {\n    socket._parent._httpMessage = this;\n    socket._parent._httpMessageDetached = false;\n  }\n  socket.on(\"close\", onServerResponseClose);\n  this.socket = socket;\n  this.emit(\"socket\", socket);\n  this._flush();\n};\n\nServerResponse.prototype.detachSocket = function detachSocket(socket) {\n  assert(socket._httpMessage === this);\n  socket.removeListener(\"close\", onServerResponseClose);\n  socket._httpMessage = null;\n  socket._httpMessageDetached = true;\n  if (socket._parent) {","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/node/polyfills/_http_server.js#L366-L402","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Do not call assignSocket on an already-attached socket; use a newly accepted or fully detached socket","Check socket._httpMessage first and skip (or wait for its 'close'/'free' event) when it is set","If you took over a socket (e.g. after upgrade), detach the previous response / clear socket._httpMessage before re-assigning","Prefer the public server API (server.on('connection'), res.socket) over manual socket assignment"],"exampleFix":"// before\nres.assignSocket(clientSocket); // throws ERR_HTTP_SOCKET_ASSIGNED if already bound\n\n// after\nif (!clientSocket._httpMessage && !clientSocket.destroyed) {\n  res.assignSocket(clientSocket);\n}","handlingStrategy":"validation","validationCode":"function socketIsAssignable(socket) {\n  return !!socket && !socket._httpMessage && !socket.destroyed;\n}\n// before attaching\nif (socketIsAssignable(sock)) {\n  res.assignSocket(sock);\n}","typeGuard":null,"tryCatchPattern":"try {\n  res.assignSocket(sock);\n} catch (e) {\n  if (e.code === 'ERR_HTTP_SOCKET_ASSIGNED') {\n    // socket already owned by another response: pick a different socket or detach first\n  } else throw e;\n}","preventionTips":["Treat assignSocket as an internal API; prefer the server's own socket management","Track socket ownership in your pool keyed on socket._httpMessage","After upgrading or hijacking a socket, ensure the previous ServerResponse is detached before reuse"],"tags":["http","node-compat","server","sockets"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}