denoland/deno · error · Error
ERR_SOCKET_DGRAM_NOT_CONNECTED
ERR_SOCKET_DGRAM_NOT_CONNECTED
Error message
Not connected
What it means
Socket.disconnect() only works from the CONNECT_STATE_CONNECTED state; calling it while DISCONNECTED or still CONNECTING throws ERR_SOCKET_DGRAM_NOT_CONNECTED. This matches the method's own doc comment: disconnecting an unbound or already-disconnected socket throws.
Source
Thrown at ext/node/polyfills/dgram.ts:652
);
return;
}
ReflectApply(_connect, this, [port, address, callback]);
}
/**
* A synchronous function that disassociates a connected `dgram.Socket` from
* its remote address. Trying to call `disconnect()` on an unbound or already
* disconnected socket will result in an `ERR_SOCKET_DGRAM_NOT_CONNECTED`
* exception.
*/
disconnect() {
const state = this[kStateSymbol];
if (state.connectState !== CONNECT_STATE_CONNECTED) {
throw new ERR_SOCKET_DGRAM_NOT_CONNECTED();
}
const err = state.handle!.disconnect();
if (err) {
throw errnoException(err, "connect");
} else {
state.connectState = CONNECT_STATE_DISCONNECTED;
}
}
/**
* Instructs the kernel to leave a multicast group at `multicastAddress`
* using the `IP_DROP_MEMBERSHIP` socket option. This method is automatically
* called by the kernel when the socket is closed or the process terminates,
* so most apps will never have reason to call this.
*
* If `multicastInterface` is not specified, the operating system willView on GitHub (pinned to 89f33cbef2)
Solutions
- Only disconnect after the 'connect' event has fired
- Track connection state yourself and skip redundant disconnects
- During teardown, close() alone is safe — disconnect is optional
Example fix
// before
function stop() { sock.disconnect(); }
// after
let connected = false;
sock.once("connect", () => (connected = true));
function stop() { if (connected) sock.disconnect(); } Defensive patterns
Strategy: validation
Validate before calling
let connected = false;
sock.once("connect", () => { connected = true; });
sock.on("close", () => { connected = false; });
function safeDisconnect() {
if (connected) { sock.disconnect(); connected = false; }
} Try / catch
try { sock.disconnect(); }
catch (e) {
if (e.code === "ERR_SOCKET_DGRAM_NOT_CONNECTED") { /* fine — nothing to disconnect */ }
else throw e;
} Prevention
- Gate disconnect on the 'connect' event, never call it speculatively
- Make teardown idempotent
- Wait for 'connect' or 'error' before disconnecting a pending connect
When it happens
Trigger: disconnect() before any connect(); double disconnect(); disconnect() while the asynchronous connect is still in progress (state CONNECTING, not yet CONNECTED).
Common situations: Cleanup paths (finally blocks) that disconnect unconditionally; health-check loops that reset state every tick; reconnect flows that disconnect even though the previous connect never completed.
Related errors
- ERR_SOCKET_ALREADY_BOUND
- ERR_SOCKET_DGRAM_IS_CONNECTED
- ERR_MISSING_ARGS
- ERR_INVALID_FD_TYPE
- ERR_SOCKET_BAD_BUFFER_SIZE
AI-assisted analysis of denoland/deno@89f33cbef2 (2026-08-16).
Data as JSON: /api/errors/61265d18556a42cf.
Report an issue: GitHub.