{"record":{"id":"252bea18358e235a","repo":"nodejs/node","slug":"und-err-socket-252bea","errorCode":"UND_ERR_SOCKET","errorMessage":"destroyed","messagePattern":"destroyed","errorType":"exception","errorClass":"SocketError","httpStatus":null,"severity":"error","filePath":"deps/undici/src/lib/dispatcher/client-h1.js","lineNumber":873,"sourceCode":"\n/**\n * @param {import ('./client.js')} client\n * @param {import('net').Socket} socket\n * @returns\n */\nfunction connectH1 (client, socket) {\n  client[kSocket] = socket\n\n  if (!llhttpInstance) {\n    llhttpInstance = lazyllhttp()\n  }\n\n  if (socket.errored) {\n    throw socket.errored\n  }\n\n  if (socket.destroyed) {\n    throw new SocketError('destroyed')\n  }\n\n  socket[kNoRef] = false\n  socket[kWriting] = false\n  socket[kReset] = false\n  socket[kBlocking] = false\n  socket[kIdleSocketValidation] = 0\n  socket[kIdleSocketValidationTimeout] = null\n  socket[kSocketUsed] = false\n  socket[kParser] = new Parser(client, socket, llhttpInstance)\n\n  util.addListener(socket, 'error', onHttpSocketError)\n  util.addListener(socket, 'readable', onHttpSocketReadable)\n  util.addListener(socket, 'end', onHttpSocketEnd)\n  util.addListener(socket, 'close', onHttpSocketClose)\n\n  socket[kClosed] = false\n  socket.on('close', onSocketClose)","sourceCodeStart":855,"sourceCodeEnd":891,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/deps/undici/src/lib/dispatcher/client-h1.js#L855-L891","documentation":"Thrown inside connectH1() (the HTTP/1.1 socket setup in client-h1.js) as a SocketError (code UND_ERR_SOCKET) with message 'destroyed'. After attaching a socket to the client, connectH1 inspects socket.errored and socket.destroyed; if the socket was already torn down (destroyed === true) it cannot be wired to a parser and is rejected. This usually surfaces the real socket lifecycle problem as a typed error.","triggerScenarios":"A connection attempt that completes after the socket was destroyed (e.g. an aborted request, a TLS error, a timeout firing first, or a manual client.destroy() racing the connect); reuse of a socket that the pool already torn down.","commonSituations":"Calling client.destroy() then dispatching before close completes; an interceptor aborting the request mid-connect; keep-alive sockets reaped by the server sent FIN and were destroyed locally just as a new request picked them; Node shutdown aborting in-flight connects.","solutions":["Do not dispatch on a destroyed/closing client — check client.closed / client.destroyed first.","If you aborted, await client.close() or the abort completion before reusing the client.","Investigate the upstream socket.errored (it is thrown when present) — TLS/cert or ECONNREFUSED often cause the destroy.","Retry the request on a fresh client when UND_ERR_SOCKET/destroyed is caught, ideally via a RetryAgent."],"exampleFix":"// before\nclient.destroy()\nawait client.request({ method: 'GET', path: '/' }) // may throw 'destroyed'\n// after\nawait client.close()\nconst client2 = new Client(origin)\nawait client2.request({ method: 'GET', path: '/' })","handlingStrategy":"retry","validationCode":"function isUsable(client) {\n  return !client.closed && !client.destroyed\n}\nif (!isUsable(client)) client = new Client(origin)","typeGuard":"const isSocketDestroyed = (e) => e?.code === 'UND_ERR_SOCKET' && /destroyed/.test(e.message ?? '')","tryCatchPattern":"try {\n  await client.request(req)\n} catch (e) {\n  if (e?.code === 'UND_ERR_SOCKET') {\n    // recreate client and retry idempotent requests once\n  } else throw e\n}","preventionTips":["Never dispatch on a destroyed/closing client; await close() first.","Use RetryAgent to replay idempotent requests on socket errors.","Investigate socket.errored — the underlying TLS/connect failure is the real cause."],"tags":["undici","client","socket","destroyed","lifecycle","network"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}