denoland/deno · error · NodeError
ERR_SOCKET_DGRAM_IS_CONNECTED
ERR_SOCKET_DGRAM_IS_CONNECTED
Error message
Already connected
What it means
Socket.prototype.connect() associates the unconnected socket with one remote address. If the connect state is anything other than DISCONNECTED (already connected, or a connect still pending), it throws ERR_SOCKET_DGRAM_IS_CONNECTED.
Solutions
- Call disconnect() before you connect() to a new endpoint.
- Track connected state yourself and skip redundant connects.
- Chain endpoint changes: disconnect, wait for completion, then connect.
Example fix
// before socket.connect(newPort, newHost); // already connected // after if (connected) socket.disconnect(); socket.connect(newPort, newHost);
Defensive patterns
Strategy: validation
Validate before calling
let connected = false;
socket.once('connect', () => { connected = true; });
function reconnect(port: number, host: string) { if (connected) socket.disconnect(); socket.connect(port, host); } Try / catch
try { socket.connect(port, host); } catch (e) { if (e.code === 'ERR_SOCKET_DGRAM_IS_CONNECTED') { socket.disconnect(); socket.connect(port, host); } else throw e; } Prevention
- Track connect state with a flag set on the 'connect' event.
- Always pair endpoint changes with disconnect() first.
When it happens
Trigger: Calling connect() twice in a row. A connect() issued while an earlier connect() has not completed. Reconnect logic that calls connect() without disconnect() first.
Common situations: Failover code that switches remote endpoints without disconnecting. Event handlers that connect on every message. Two startup paths that both connect the same socket.
Related errors
- ERR_SOCKET_ALREADY_BOUND
- ERR_SOCKET_DGRAM_NOT_CONNECTED
- ERR_BUFFER_OUT_OF_BOUNDS
- ERR_INVALID_ARG_TYPE
- ERR_INVALID_FD_TYPE
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/0348a5bc2761d1b8.
Report an issue: GitHub.
Appendix: source
Thrown at ext/node/polyfills/dgram.ts:614
callback?: (err?: ErrnoException) => void,
): void;
connect(port: number, callback: (err?: ErrnoException) => void): void;
connect(port: number, address?: unknown, callback?: unknown) {
port = validatePort(port, "Port", false);
if (typeof address === "function") {
callback = address;
address = "";
} else if (address === undefined) {
address = "";
}
validateString(address, "address");
const state = this[kStateSymbol];
if (state.connectState !== CONNECT_STATE_DISCONNECTED) {
throw new ERR_SOCKET_DGRAM_IS_CONNECTED();
}
state.connectState = CONNECT_STATE_CONNECTING;
if (state.bindState === BIND_STATE_UNBOUND) {
// deno-lint-ignore deno-internal/prefer-primordials -- Socket's own bind method, not Function.prototype.bind
this.bind({ port: 0, exclusive: true });
}
if (state.bindState !== BIND_STATE_BOUND) {
enqueue(
this,
FunctionPrototypeBind(
_connect,
this,
port,
address as string,
callback as (err?: ErrnoException) => void,View on GitHub (pinned to 9ad36f7a2c)