{"record":{"id":"5f32e409841a89b4","repo":"denoland/deno","slug":"socket-is-not-a-tcp-socket-only-tcp-connections","errorCode":null,"errorMessage":"Socket is not a TCP socket - only TCP connections can be upgraded to WebSocket","messagePattern":"Socket is not a TCP socket - only TCP connections can be upgraded to WebSocket","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/http/02_websocket.ts","lineNumber":165,"sourceCode":"    // sync with the header-list built above (protocol negotiation, etc.).\n    let responseHead = \"HTTP/1.1 101 Switching Protocols\\r\\n\";\n    for (let i = 0; i < r.headerList.length; i++) {\n      const { 0: name, 1: value } = r.headerList[i];\n      responseHead += `${name}: ${value}\\r\\n`;\n    }\n    responseHead += \"\\r\\n\";\n\n    if (nodeSocket.destroyed) {\n      throw new TypeError(\n        \"Socket is already destroyed - cannot upgrade to WebSocket\",\n      );\n    }\n    const handle = nodeSocket._handle;\n    if (!handle) {\n      throw new TypeError(\"Socket has no handle - cannot upgrade\");\n    }\n    if (typeof handle.takeStream !== \"function\") {\n      throw new TypeError(\n        \"Socket is not a TCP socket - only TCP connections can be upgraded to WebSocket\",\n      );\n    }\n\n    // Extra bytes that were already buffered (e.g., from the upgrade\n    // request body that arrived with the headers)\n    const extraBytes = options.head || new Uint8Array(0);\n\n    // Defer setup so the caller can attach event handlers (onopen,\n    // onmessage, etc.) before events fire.\n    (async () => {\n      try {\n        // Wait for the 101 response to fully flush before taking the\n        // stream. A fire-and-forget write could leave data in the\n        // internal_write_queue that would be orphaned once we detach\n        // the stream from libuv.\n        await new Promise((resolve, reject) => {\n          nodeSocket.write(responseHead, (err) => {","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/http/02_websocket.ts#L147-L183","documentation":"To turn the node socket into a WebSocket, Deno calls `takeStream()` on the socket's native handle — a method only TCP handles expose in Deno's node compatibility layer. TLS-wrapped sockets (`TLSSocket`), Unix-domain/named-pipe sockets, and IPC channels do not have it, so the upgrade is refused with this TypeError rather than corrupting the connection.","triggerScenarios":"Invoking the node upgrade path on a connection from a `tls.Server` (`secureConnection`/TLS-wrapped socket); serving on `server.listen({ path: '/tmp/app.sock' })` (pipe/UDS) and upgrading; upgrading a child_process IPC channel socket.","commonSituations":"HTTPS/WSS setups expecting the same upgrade flow as plain HTTP; microservices communicating over Unix domain sockets; polyglot setups piping Node servers.","solutions":["Serve WebSocket upgrades on a plain TCP listener (terminate TLS at a proxy in front if needed)","For secure WS, use Deno's native `Deno.serve` over TLS with `Deno.upgradeWebSocket`, which does not go through the node socket path","Check `typeof socket._handle?.takeStream === 'function'` before attempting the upgrade"],"exampleFix":"// before\nconst tcpServer = nodeNet.createServer();\ntcpServer.listen({ path: \"/tmp/app.sock\" });\ntcpServer.on(\"upgrade\", (req, socket) => {\n  Deno.upgradeWebSocket(req, { socket }); // UDS socket -> TypeError\n});\n\n// after\nconst tcpServer = nodeNet.createServer();\ntcpServer.listen({ port: 8080 }); // plain TCP\nconst httpServer = nodeHttp.createServer();\nhttpServer.on(\"upgrade\", (req, socket) => {\n  Deno.upgradeWebSocket(req, { socket }); // TCP handle has takeStream\n});","handlingStrategy":"validation","validationCode":"const handle = (socket as any)._handle;\nif (typeof handle?.takeStream !== \"function\") {\n  socket.destroy(); // not a TCP socket; cannot become a WebSocket\n  return;\n}\nconst { response } = Deno.upgradeWebSocket(req, { socket });","typeGuard":"const isTcpNodeSocket = (\n  s: { destroyed?: boolean; _handle?: { takeStream?: unknown } },\n): boolean =>\n  !s.destroyed && !!s._handle && typeof s._handle.takeStream === \"function\";","tryCatchPattern":null,"preventionTips":["Only attach the node 'upgrade' path to plain TCP listeners","Terminate TLS in front (proxy or Deno.serve over TLS) for secure WS instead of upgrading TLSSocket","Do not offer WebSocket on Unix-domain-socket or IPC servers"],"tags":["websocket","node-http","tls","unix-socket","transport"],"backgroundTag":"unsupported-socket-type","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}