denoland/deno · error · NodeError

ERR_SOCKET_DGRAM_NOT_RUNNING

ERR_SOCKET_DGRAM_NOT_RUNNING

Error message

Not running

What it means

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.

Solutions

  1. Maintain a closed flag via socket.once('close', () => { closed = true; }) and check it before any operation
  2. Cancel pending timers and pending callbacks before calling close()
  3. Create a new socket instead of reusing a closed one

Example fix

// before
setTimeout(() => socket.send(beat, PORT, HOST), 1000);
socket.close(); // timer later throws ERR_SOCKET_DGRAM_NOT_RUNNING

// after
const timer = setTimeout(() => socket.send(beat, PORT, HOST), 1000);
socket.close();
clearTimeout(timer);
Defensive patterns

Strategy: validation

Validate before calling

let closed = false;
socket.once('close', () => { closed = true; });
function safeSend(buf, port, address) {
  if (closed) return false;
  socket.send(buf, port, address);
  return true;
}

Try / catch

try {
  socket.setTTL(64);
} catch (err) {
  if (err?.code === 'ERR_SOCKET_DGRAM_NOT_RUNNING') return; // already closed
  throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/000b6a59bd7470a8. Report an issue: GitHub.

Appendix: source

Thrown at ext/node/polyfills/dgram.ts:1423

  const ctx = {};
  const ret = self[kStateSymbol].handle!.bufferSize(size, buffer, ctx);

  if (ret === undefined) {
    throw new ERR_SOCKET_BUFFER_SIZE(ctx as NodeSystemErrorCtx);
  }

  return ret;
}

function socketCloseNT(self: Socket) {
  self.emit("close");
}

function healthCheck(socket: Socket) {
  if (!socket[kStateSymbol].handle) {
    // Error message from dgram_legacy.js.
    throw new ERR_SOCKET_DGRAM_NOT_RUNNING();
  }
}

function stopReceiving(socket: Socket) {
  const state = socket[kStateSymbol];

  if (!state.receiving) {
    return;
  }

  state.handle!.recvStop();
  state.receiving = false;
}

function onMessage(
  nread: number,
  handle: UDP,
  buf?: Buffer,

View on GitHub (pinned to 9ad36f7a2c)