{"record":{"id":"9669c01d7ef3fee6","repo":"denoland/deno","slug":"err-socket-already-bound","errorCode":"ERR_SOCKET_ALREADY_BOUND","errorMessage":"Socket is already bound","messagePattern":"Socket is already bound","errorType":"exception","errorClass":"NodeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/dgram.ts","lineNumber":413,"sourceCode":"   * server.bind(41234);\n   * // Prints: server listening 0.0.0.0:41234\n   * ```\n   *\n   * @param callback with no parameters. Called when binding is complete.\n   */\n  bind(port?: number, address?: string, callback?: () => void): this;\n  bind(port: number, callback?: () => void): this;\n  bind(callback: () => void): this;\n  bind(options: BindOptions, callback?: () => void): this;\n  bind(port_?: unknown, address_?: unknown /* callback */): this {\n    let port = typeof port_ === \"function\" ? null : port_;\n\n    healthCheck(this);\n\n    const state = this[kStateSymbol];\n\n    if (state.bindState !== BIND_STATE_UNBOUND) {\n      throw new ERR_SOCKET_ALREADY_BOUND();\n    }\n\n    state.bindState = BIND_STATE_BINDING;\n\n    const cb = arguments.length && arguments[arguments.length - 1];\n\n    if (typeof cb === \"function\") {\n      // deno-lint-ignore no-inner-declarations\n      function removeListeners(this: Socket) {\n        this.removeListener(\"error\", removeListeners);\n        this.removeListener(\"listening\", onListening);\n      }\n\n      // deno-lint-ignore no-inner-declarations\n      function onListening(this: Socket) {\n        FunctionPrototypeCall(removeListeners, this);\n        FunctionPrototypeCall(cb, this);\n      }","sourceCodeStart":395,"sourceCodeEnd":431,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/dgram.ts#L395-L431","documentation":"Socket.prototype.bind() guards a small state machine (UNBOUND, BINDING, BOUND). A second bind() call on a socket whose state is not UNBOUND throws ERR_SOCKET_ALREADY_BOUND. This includes a call made while a first bind is still in progress.","triggerScenarios":"socket.bind(41234) followed later by socket.bind(41235). A bind() inside retry or reconnect logic. Two modules that each bind the same shared socket.","commonSituations":"Reconnect flows that rebind instead of creating a new socket. Race between an explicit bind() and a bind() scheduled by earlier code. Cleanup code that re-initializes by binding again.","solutions":["Bind each socket exactly once; track it with your own flag or the 'listening' event.","To listen on a new port, create a new socket with createSocket() instead of rebinding.","Remove bind() from retry paths; bind after a close only on a fresh socket."],"exampleFix":"// before\nsocket.bind(port);\n// later, on retry:\nsocket.bind(port); // ERR_SOCKET_ALREADY_BOUND\n\n// after\nsocket.close();\nsocket = dgram.createSocket('udp4');\nsocket.bind(port);","handlingStrategy":"try-catch","validationCode":"let bound = false;\nsocket.once('listening', () => { bound = true; });\nfunction bindOnce(port: number) { if (bound) return; socket.bind(port); }","typeGuard":null,"tryCatchPattern":"try { socket.bind(port); } catch (e) { if (e.code === 'ERR_SOCKET_ALREADY_BOUND') { socket.close(); socket = dgram.createSocket('udp4'); socket.bind(port); } else throw e; }","preventionTips":["Bind in exactly one place, behind a wrapper with a bound flag.","On rebind, create a new socket rather than reusing the bound one."],"tags":["dgram","udp","bind","socket-state"],"backgroundTag":"socket-already-bound","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"}