{"record":{"id":"000b6a59bd7470a8","repo":"denoland/deno","slug":"err-socket-dgram-not-running","errorCode":"ERR_SOCKET_DGRAM_NOT_RUNNING","errorMessage":"Not running","messagePattern":"Not running","errorType":"exception","errorClass":"NodeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/dgram.ts","lineNumber":1423,"sourceCode":"\n  const ctx = {};\n  const ret = self[kStateSymbol].handle!.bufferSize(size, buffer, ctx);\n\n  if (ret === undefined) {\n    throw new ERR_SOCKET_BUFFER_SIZE(ctx as NodeSystemErrorCtx);\n  }\n\n  return ret;\n}\n\nfunction socketCloseNT(self: Socket) {\n  self.emit(\"close\");\n}\n\nfunction healthCheck(socket: Socket) {\n  if (!socket[kStateSymbol].handle) {\n    // Error message from dgram_legacy.js.\n    throw new ERR_SOCKET_DGRAM_NOT_RUNNING();\n  }\n}\n\nfunction stopReceiving(socket: Socket) {\n  const state = socket[kStateSymbol];\n\n  if (!state.receiving) {\n    return;\n  }\n\n  state.handle!.recvStop();\n  state.receiving = false;\n}\n\nfunction onMessage(\n  nread: number,\n  handle: UDP,\n  buf?: Buffer,","sourceCodeStart":1405,"sourceCodeEnd":1441,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/dgram.ts#L1405-L1441","documentation":"Most dgram methods call healthCheck() first, which throws ERR_SOCKET_DGRAM_NOT_RUNNING when the internal handle is gone — i.e. the socket was close()d. After close, operations such as send, bind, setTTL, addMembership and address() can no longer run.","triggerScenarios":"A setTimeout or in-flight promise firing after socket.close(); reusing a socket variable after the 'close' event; shutdown handlers running twice; an error handler closing the socket while queued sends drain.","commonSituations":"Races between async sends and shutdown; duplicate cleanup in signal handlers; long-lived maps of sockets where some entries were closed; tests that close sockets in afterEach while handlers still fire.","solutions":["Maintain a closed flag via socket.once('close', () => { closed = true; }) and check it before any operation","Cancel pending timers and pending callbacks before calling close()","Create a new socket instead of reusing a closed one"],"exampleFix":"// before\nsetTimeout(() => socket.send(beat, PORT, HOST), 1000);\nsocket.close(); // timer later throws ERR_SOCKET_DGRAM_NOT_RUNNING\n\n// after\nconst timer = setTimeout(() => socket.send(beat, PORT, HOST), 1000);\nsocket.close();\nclearTimeout(timer);","handlingStrategy":"validation","validationCode":"let closed = false;\nsocket.once('close', () => { closed = true; });\nfunction safeSend(buf, port, address) {\n  if (closed) return false;\n  socket.send(buf, port, address);\n  return true;\n}","typeGuard":null,"tryCatchPattern":"try {\n  socket.setTTL(64);\n} catch (err) {\n  if (err?.code === 'ERR_SOCKET_DGRAM_NOT_RUNNING') return; // already closed\n  throw err;\n}","preventionTips":["Flip a closed flag on the 'close' event and check it before any socket call","Clear timers and settle in-flight callbacks before close()","Never reuse a closed socket — create a fresh one for reconnects"],"tags":["dgram","udp","lifecycle","socket-closed"],"backgroundTag":"socket-closed","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"}