denoland/deno · error · NodeError
ERR_SOCKET_ALREADY_BOUND
ERR_SOCKET_ALREADY_BOUND
Error message
Socket is already bound
What it means
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.
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.
Example fix
// before
socket.bind(port);
// later, on retry:
socket.bind(port); // ERR_SOCKET_ALREADY_BOUND
// after
socket.close();
socket = dgram.createSocket('udp4');
socket.bind(port); Defensive patterns
Strategy: try-catch
Validate before calling
let bound = false;
socket.once('listening', () => { bound = true; });
function bindOnce(port: number) { if (bound) return; socket.bind(port); } Try / catch
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; } Prevention
- Bind in exactly one place, behind a wrapper with a bound flag.
- On rebind, create a new socket rather than reusing the bound one.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- ERR_SOCKET_DGRAM_IS_CONNECTED
- 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/9669c01d7ef3fee6.
Report an issue: GitHub.
Appendix: source
Thrown at ext/node/polyfills/dgram.ts:413
* server.bind(41234);
* // Prints: server listening 0.0.0.0:41234
* ```
*
* @param callback with no parameters. Called when binding is complete.
*/
bind(port?: number, address?: string, callback?: () => void): this;
bind(port: number, callback?: () => void): this;
bind(callback: () => void): this;
bind(options: BindOptions, callback?: () => void): this;
bind(port_?: unknown, address_?: unknown /* callback */): this {
let port = typeof port_ === "function" ? null : port_;
healthCheck(this);
const state = this[kStateSymbol];
if (state.bindState !== BIND_STATE_UNBOUND) {
throw new ERR_SOCKET_ALREADY_BOUND();
}
state.bindState = BIND_STATE_BINDING;
const cb = arguments.length && arguments[arguments.length - 1];
if (typeof cb === "function") {
// deno-lint-ignore no-inner-declarations
function removeListeners(this: Socket) {
this.removeListener("error", removeListeners);
this.removeListener("listening", onListening);
}
// deno-lint-ignore no-inner-declarations
function onListening(this: Socket) {
FunctionPrototypeCall(removeListeners, this);
FunctionPrototypeCall(cb, this);
}View on GitHub (pinned to 9ad36f7a2c)