{"record":{"id":"61265d18556a42cf","repo":"denoland/deno","slug":"err-socket-dgram-not-connected","errorCode":"ERR_SOCKET_DGRAM_NOT_CONNECTED","errorMessage":"Not connected","messagePattern":"Not connected","errorType":"exception","errorClass":"NodeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/dgram.ts","lineNumber":652,"sourceCode":"      );\n\n      return;\n    }\n\n    ReflectApply(_connect, this, [port, address, callback]);\n  }\n\n  /**\n   * A synchronous function that disassociates a connected `dgram.Socket` from\n   * its remote address. Trying to call `disconnect()` on an unbound or already\n   * disconnected socket will result in an `ERR_SOCKET_DGRAM_NOT_CONNECTED`\n   * exception.\n   */\n  disconnect() {\n    const state = this[kStateSymbol];\n\n    if (state.connectState !== CONNECT_STATE_CONNECTED) {\n      throw new ERR_SOCKET_DGRAM_NOT_CONNECTED();\n    }\n\n    const err = state.handle!.disconnect();\n\n    if (err) {\n      throw errnoException(err, \"connect\");\n    } else {\n      state.connectState = CONNECT_STATE_DISCONNECTED;\n    }\n  }\n\n  /**\n   * Instructs the kernel to leave a multicast group at `multicastAddress`\n   * using the `IP_DROP_MEMBERSHIP` socket option. This method is automatically\n   * called by the kernel when the socket is closed or the process terminates,\n   * so most apps will never have reason to call this.\n   *\n   * If `multicastInterface` is not specified, the operating system will","sourceCodeStart":634,"sourceCodeEnd":670,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/dgram.ts#L634-L670","documentation":"Socket.prototype.disconnect() only works on a socket whose connect state is CONNECTED. Any other state, including never connected and connect still in progress, throws ERR_SOCKET_DGRAM_NOT_CONNECTED. On success the state returns to DISCONNECTED.","triggerScenarios":"disconnect() called before any connect(). A second disconnect() after a successful one. disconnect() issued while the initial connect() is still pending.","commonSituations":"Cleanup routines that call disconnect() unconditionally on shutdown. Reset paths that run before the socket finished connecting. Idempotent teardown code written without state tracking.","solutions":["Track the connect state with a flag set on the 'connect' event.","Only call disconnect() on that flag.","Wrap teardown disconnect() in try-catch so shutdown never breaks."],"exampleFix":"// before\nfunction shutdown() { socket.disconnect(); } // throws when never connected\n\n// after\nfunction shutdown() { if (isConnected) socket.disconnect(); }","handlingStrategy":"validation","validationCode":"let connected = false;\nsocket.once('connect', () => { connected = true; });\nsocket.on('close', () => { connected = false; });\nfunction teardown() { if (connected) socket.disconnect(); }","typeGuard":null,"tryCatchPattern":"try { socket.disconnect(); } catch (e) { if (e.code === 'ERR_SOCKET_DGRAM_NOT_CONNECTED') { /* nothing to do */ } else throw e; }","preventionTips":["Make teardown idempotent with a connected flag.","Never call disconnect() from paths that can run before 'connect' fires."],"tags":["dgram","udp","disconnect","socket-state"],"backgroundTag":"not-connected","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"}