EADDRINUSE
Linux / POSIXERRORCommonNetworkHIGH confidence

Address Already in Use

What this means

Another socket is already bound to the specified address and port. The port may be in use by another process, or the socket may be in TCP TIME_WAIT state after a recent connection, preventing immediate reuse.

Why it happens
  1. 1Another process is already listening on the same port.
  2. 2The process crashed and restarted before the TCP TIME_WAIT period expired (default 60 seconds).
  3. 3Two instances of the same service were started simultaneously.
  4. 4The port was bound by a previous test run that is still in TIME_WAIT.
How to reproduce

Starting a second server process on the same port.

trigger — this will error
trigger — this will error
$ node server.js &
$ node server.js
Error: listen EADDRINUSE: address already in use :::3000

expected output

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (node:net:1380:16)

Fix 1

Set SO_REUSEADDR before binding

WHEN For server sockets that need to restart quickly without waiting for TIME_WAIT

Set SO_REUSEADDR before binding
// Node.js: use server.listen() with reuseAddr option
// In C:
int opt = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));

Why this works

SO_REUSEADDR allows binding to a port that is in TIME_WAIT state, enabling fast server restarts.

Fix 2

Find and stop the process using the port

WHEN When the port is being used by a different process

Find and stop the process using the port
# Find who is using port 3000
sudo ss -tlnp | grep :3000
sudo lsof -i :3000

# Kill the process
kill $(lsof -t -i :3000)

Why this works

Stopping the process that owns the port releases the binding, allowing the new process to bind.

Sources
Official documentation ↗

Linux Programmer Manual errno(3)

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Linux / POSIX errors