HTTP socket already assigned to another response
Production Risk
Low — only affects code that manually manages HTTP sockets.
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.
- 1Manually calling response.assignSocket() more than once on the same response
- 2Custom socket management code that attempts to reassign sockets
Triggered when assignSocket() is called on an HTTP message that already has an assigned socket.
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
if (!res.socket) {
res.assignSocket(mySocket);
}Why this works
Guarding on the existing socket property prevents duplicate assignment.
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_ASSIGNEDtry {
// 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
}
}// Validate inputs before calling the operation
function safe_err_http_socket_assigned(...args) {
// validate args here
return performOperation(...args)
}✕ Call assignSocket() on a response that was created by the HTTP server
The server assigns sockets automatically; manual reassignment is unnecessary and errors.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev