Fix 'Address already in use' (EADDRINUSE) with SO_REUSEADDR
Fix bind failing with Address already in use (EADDRINUSE) on Linux by setting SO_REUSEADDR before bind, and learn when another process holds the port.

The error text is exact: bind: Address already in use. It comes from the bind call, the errno is EADDRINUSE, and it stops a server from starting. Two quite different causes produce it, and the fix for one is wrong for the other. This post shows how to tell them apart on Linux 6.12, why SO_REUSEADDR fixes the common case, and what to do when the port is genuinely taken.
The option is documented in socket(7), and the error in bind(2).
The error as it appears
A server that calls bind on a port that is unavailable prints:
bind: Address already in useThe code that produced it looks like this, using the usual perror pattern:
if (bind(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
perror("bind");
return EXIT_FAILURE;
}The message comes from strerror(EADDRINUSE). In Python it appears as OSError: [Errno 98] Address already in use, and the same number 98 appears on Linux in every language. The code means that some socket already holds a conflicting local address and port.
Cause one: another process is listening
The simplest cause is that another program, or an earlier copy of yours, is still running and listening on that port. Check before you change any code. Run ss -ltnp and look for the port:
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 16 0.0.0.0:7070 0.0.0.0:* users:(("echo_server",pid=4121,fd=3))A line like that names the process. No socket option can fix this case, and SO_REUSEADDR will not let two programs listen on the same TCP address and port. Stop the other process or choose another port. If you do want several processes to share one port for load balancing, that is SO_REUSEPORT, a different option with different rules, covered in its own post.
Cause two: connections in TIME_WAIT
The second cause is the one that makes people think the error is random. The server was stopped moments ago, ss -ltnp shows nothing listening, and yet bind fails. The reason is that closed connections from the previous run are still in the TIME_WAIT state. When the side that closes first finishes a TCP connection, the kernel keeps the connection record for 60 seconds on Linux, so late packets from the old connection cannot be mistaken for a new one. Those records still count as using the local port.
List them with ss -tan state time-wait:
Recv-Q Send-Q Local Address:Port Peer Address:Port
0 0 127.0.0.1:7070 127.0.0.1:52814A server that stops while clients are connected, and therefore closes first, leaves exactly these entries. After about a minute they disappear and bind succeeds again. That is why the failure seems to go away on its own.
The fix: SO_REUSEADDR
Setting SO_REUSEADDR before bind tells the kernel that you accept sharing the address with leftover TIME_WAIT connections. On Linux it allows bind to succeed when no socket is actively listening on the address, even though old connections linger. The call goes between socket and bind:
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
int main(void)
{
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) { perror("socket"); return EXIT_FAILURE; }
int yes = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) {
perror("setsockopt");
return EXIT_FAILURE;
}
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(7070);
if (bind(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
perror("bind");
return EXIT_FAILURE;
}
if (listen(fd, 16) < 0) {
perror("listen");
return EXIT_FAILURE;
}
printf("listening on port 7070\n");
close(fd);
return EXIT_SUCCESS;
}Compile with gcc -std=c11 -Wall -Wextra -o reuse reuse.c. Run it twice in a row, even with connections in TIME_WAIT from an earlier server:
listening on port 7070
listening on port 7070Without the setsockopt call, the second run can instead print bind: Address already in use. The order matters: the option has to be set before bind, because setting it afterwards does nothing for that call.
What SO_REUSEADDR does not do
It is easy to over-trust the option. On Linux it does not let two sockets listen on the same address and port at the same time. If another process is in the LISTEN state, bind still fails. This differs from some other systems, where the same option name can allow exactly that. Do not carry assumptions across platforms: on the BSD family and on Windows the semantics differ, so check the manual of each target.
It also does not make a bad design acceptable. Binding the wildcard address 0.0.0.0 conflicts with any specific address on the same port, and the option changes that only when no socket is listening. A server that is meant to run as several instances should use distinct ports or SO_REUSEPORT.
Other ways to hit the same error
Three more situations produce EADDRINUSE, and none of them is a timing problem:
A container or another network namespace is publishing the port on the host.
ssinside your own namespace shows nothing, but the host's port mapping holds it.Your program calls
bindtwice, for example once for IPv4 and again with an IPv6 dual-stack socket on the same port.A client's ephemeral port range includes your server port, so an outgoing connection took it before the server started. Check
net.ipv4.ip_local_port_rangeand keep server ports outside it.
For the third case, net.ipv4.ip_local_reserved_ports lets you reserve specific ports so the kernel never gives them to outgoing connections.
Why the kernel keeps TIME_WAIT at all
It is tempting to treat TIME_WAIT as a defect, but it protects correctness. TCP identifies a connection by four values. If a new connection reused the same four values right after the old one closed, a delayed segment from the old connection could arrive and be accepted as data for the new one. Holding the record for twice the maximum segment lifetime makes sure such stragglers have died out. On Linux that period is fixed at 60 seconds in the kernel source, so there is no sysctl that shortens it, and advice to tune it away is usually about a different setting.
The side that closes first is the side that ends up in TIME_WAIT. A server that closes client connections itself therefore collects these entries, while one that waits for clients to close does not. This is why the problem appears when you stop a busy server with Ctrl-C: the process exits, the kernel closes every connection on its behalf, and each one leaves a record. The entries belong to the kernel and not to your process, so they survive the exit.
Why the option is named reuse
The name describes what happens at bind time. Normally the kernel refuses to bind a local address and port while any socket, in any state, still uses that pair. With SO_REUSEADDR the kernel relaxes that check for sockets that are not listening, which covers connections in TIME_WAIT. That is the whole effect for TCP servers on Linux. The listening socket you create is still unique, and the old connections finish their wait quietly in the background without interfering with the new server.
A checklist when you see the error
Run
ss -ltnpand look for a listener on the port. If present, stop it or change the port.Run
ss -tan state time-waitand look for leftovers. If present, setSO_REUSEADDR.Confirm the option is set before
bind, and that its return value is checked.Check for a second
bindin your own code, or a second instance started by a supervisor.Check containers and port mappings on the host.


