{"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":"Error","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/89f33cbef296a2b287f323d42de54c871fa69c77/ext/node/polyfills/dgram.ts#L1405-L1441","documentation":"healthCheck() runs before dgram socket operations (addMembership, dropMembership, setMulticastInterface, setTTL-family calls, the send path, etc.) and throws ERR_SOCKET_DGRAM_NOT_RUNNING when the internal handle is null — i.e. the socket is not bound/running (no bind() yet) or has already been closed. The message mirrors Node's classic dgram 'Not running' error.","triggerScenarios":"Calling socket.addMembership('224.0.0.114') before socket.bind(); calling setMulticastTTL/setTTL/setBroadcast/setMulticastInterface on a closed socket; sending or tuning socket options after socket.close().","commonSituations":"Multicast setup code that assumes bind happened synchronously (it did not — bind is async); reusing a socket object after close in request-scoped code; setup callbacks racing a shutdown flag.","solutions":["bind the socket first and do multicast/TTL configuration inside the 'listening' event callback","Guard shutdown paths: set a closed flag in the 'close' handler and skip socket calls after it","Re-create the socket (and re-bind) instead of reusing a closed one"],"exampleFix":"// before\nconst sock = dgram.createSocket('udp4');\nsock.addMembership('224.0.0.114'); // throws ERR_SOCKET_DGRAM_NOT_RUNNING: not bound yet\n\n// after\nconst sock = dgram.createSocket('udp4');\nsock.bind(0, () => {\n  sock.addMembership('224.0.0.114');\n});","handlingStrategy":"validation","validationCode":"let running = false;\nsock.on('listening', () => { running = true; });\nsock.on('close', () => { running = false; });\nfunction safely(fn, ...args) {\n  if (!running) return; // skip addMembership/setTTL/etc. when not bound or closed\n  fn.apply(sock, args);\n}","typeGuard":"const isRunning = (s) => s != null && s._handle !== null && !s.closed; // heuristic; prefer tracking 'listening'/'close'","tryCatchPattern":"catch (e) { if (e?.code === 'ERR_SOCKET_DGRAM_NOT_RUNNING') return; /* socket gone: no-op */ throw e; }","preventionTips":["Do all multicast/TTL/broadcast configuration inside the 'listening' callback","Set a closed flag in the 'close' handler and check it before every socket call","Never reuse a closed socket — recreate and rebind"],"tags":["node-compat","dgram","udp","lifecycle","multicast"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}