{"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":"Error","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/89f33cbef296a2b287f323d42de54c871fa69c77/ext/node/polyfills/dgram.ts#L634-L670","documentation":"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.","triggerScenarios":"disconnect() before any connect(); double disconnect(); disconnect() while the asynchronous connect is still in progress (state CONNECTING, not yet CONNECTED).","commonSituations":"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.","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"],"exampleFix":"// before\nfunction stop() { sock.disconnect(); }\n// after\nlet connected = false;\nsock.once(\"connect\", () => (connected = true));\nfunction stop() { if (connected) sock.disconnect(); }","handlingStrategy":"validation","validationCode":"let connected = false;\nsock.once(\"connect\", () => { connected = true; });\nsock.on(\"close\", () => { connected = false; });\nfunction safeDisconnect() {\n  if (connected) { sock.disconnect(); connected = false; }\n}","typeGuard":null,"tryCatchPattern":"try { sock.disconnect(); }\ncatch (e) {\n  if (e.code === \"ERR_SOCKET_DGRAM_NOT_CONNECTED\") { /* fine — nothing to disconnect */ }\n  else throw e;\n}","preventionTips":["Gate disconnect on the 'connect' event, never call it speculatively","Make teardown idempotent","Wait for 'connect' or 'error' before disconnecting a pending connect"],"tags":["dgram","udp","disconnect","state-machine"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}