Blog

Server won't start with Address already in use: find and stop the process using the port

Your dev server stops with Address already in use (EADDRINUSE, Errno 98). How to find and stop the process holding the port with lsof, fuser and ss, plus a server left suspended by Ctrl+Z, a clash between 127.0.0.1 and 0.0.0.0, and TIME_WAIT and SO_REUSEADDR when the port stays busy for about 60 seconds with no process behind it, all reproduced in Python and Node.js. Also covers how to check on Windows and macOS.

Diagram of three ports in a row. The middle one is blocked by an orange plug, and another plug coming from the right is stopped in front of a prohibition sign.

I ran python3 -m http.server 8000 to look at some local files, and it stopped with this at the end of a long traceback.

OSError: [Errno 98] Address already in use

Doing the same in Node.js gives Error: listen EADDRINUSE: address already in use. The language is different but the meaning is the same: the OS is refusing because “another socket is already using that combination of address and port”. In most cases, the other party is your own server that you started earlier and forgot to stop.

This article first explains how to identify and stop the process using the port, and then goes through the less obvious cases one by one, such as “I can’t find any process but it still won’t start” and “the browser gets no response, yet the port is in use”.

The output in this article was taken with Python 3.13.18, Node.js 22.23.5 and lsof 4.95.0 on Ubuntu 24.04 (Linux kernel 6.18). IPv6 is disabled in this environment, so addresses are shown in IPv4.

What the error means: as a rule, only one socket can listen on a port

A server asks the OS “I want to receive connections arriving at this port on this address” before it starts listening. The request is bind, and starting to listen is listen. If another socket is already listening on the same port, the OS rejects the later bind with the error EADDRINUSE. On Linux the error number is 98, so Python shows [Errno 98].

If you use port 8000 twice, only the second attempt fails.

$ python3 -m http.server 8000 &
$ python3 -m http.server 8000
...
  File "/usr/local/lib/python3.13/socketserver.py", line 478, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use

In Node.js it looks like this. The address at the end changes to :::8000 or 0.0.0.0:8000 depending on the address the server tried to listen on.

Error: listen EADDRINUSE: address already in use 0.0.0.0:8000
  ...
  code: 'EADDRINUSE',
  errno: -98,

Note that Permission denied (EACCES), although it looks similar, has a different cause. On Linux, regular users often cannot listen on ports 1023 and below, and on Windows this error appears for the reserved ports described later. Cannot assign requested address (EADDRNOTAVAIL, Errno 99) appears when you specify an IP address that does not exist on the machine. Only Address already in use means two programs are fighting over a port.

Finding the process using the port (Linux)

Finding it with lsof

lsof lists open files, and network sockets are included. To narrow it down to listening sockets only, specify the following.

$ lsof -i :8000 -sTCP:LISTEN -P -n
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python3 2090 root    3u  IPv4   3842      0t0  TCP *:8000 (LISTEN)

-i :8000 limits the output to sockets on port 8000, and -sTCP:LISTEN keeps only those in the listening state. -P shows port numbers as they are instead of replacing them with service names such as http-alt, and -n skips converting IP addresses to host names. The *:8000 in the NAME column means it is listening on port 8000 on all addresses.

What you want is COMMAND and PID. Check the command line that started the process with ps before deciding whether to stop it.

$ ps -o pid,stat,cmd -p 2090
  PID STAT CMD
 2090 S    python3 -m http.server 8000

Processes started by another user may not be shown with regular user privileges. If nothing appears but the port stays in use, check again with sudo lsof -i :8000 -sTCP:LISTEN -P -n.

If you only want the PID, add -t. This is handy in scripts.

$ lsof -t -i :8000 -sTCP:LISTEN
2090

Finding it with fuser or ss

On systems without lsof, fuser can also get the PID from a port.

$ fuser -v 8000/tcp
                     USER        PID ACCESS COMMAND
8000/tcp:            root       2090 F.... python3

Recent Linux distributions usually include ss, which shows the state of sockets, out of the box. Type ss -ltnp 'sport = :8000' and the process name and PID appear at the end of the line for the socket listening on port 8000, in the form users:(("python3",pid=2090,fd=3)). The options mean: -l listening only, -t TCP, -n numeric output and -p show processes.

Stopping the process you found

If it is a development server you started yourself, the surest way is to go back to the terminal where you started it and stop it with Ctrl+C. If you don’t know which terminal it is, or have already closed it, terminate it by PID.

$ kill 2090
$ lsof -i :8000 -sTCP:LISTEN -P -n
$ echo $?
1

By default, kill sends SIGTERM, which gives the program a chance to clean up and exit. Only if it is still there after a few seconds should you use kill -9 2090 (SIGKILL). SIGKILL forces the program to quit before it can delete temporary files or save data it was in the middle of writing, so there is no need to use it from the start. If the last lsof prints nothing and returns exit code 1, the listener is gone.

If the process you stopped immediately comes back with a different PID, something outside it, such as a systemd service or a container, is restarting it. Stop it through that mechanism: stop the service after checking systemctl status <service-name>, or look in the PORTS column of docker ps for a container publishing the port, such as 0.0.0.0:8000->80/tcp, and run docker stop. With Docker, lsof may show a process called docker-proxy, which is also a clue that a container is publishing the port.

Common forms of “forgot to stop it”

A server suspended with Ctrl+Z is still there

If you press Ctrl+Z when you meant Ctrl+C, the server is suspended instead of terminated. The terminal shows something like [1]+ Stopped python3 -m http.server 8000 and the prompt comes back. A suspended process still has its socket open, so starting it again gives Address already in use.

This state has a telltale sign. When you connect with a browser or curl, you don’t get “connection refused”; you just wait with no response. Because the socket is still listening, the OS accepts the connection itself, but the process that receives it is stopped and never replies. When I actually suspended the server and cut curl off after 3 seconds, the result was a timeout.

$ ps -o pid,stat,cmd -p 2090
  PID STAT CMD
 2090 T    python3 -m http.server 8000
$ timeout 3 curl -s http://127.0.0.1:8000/ ; echo "exit $?"
exit 124

The T in the STAT column marks a suspended process. If you are in the same terminal, list jobs with jobs, bring it back to the foreground with fg and end it with Ctrl+C. If you have closed the terminal, kill it by PID as in the previous section.

Still running in the background or in another terminal

Servers started with &, servers started with nohup, and servers left running in an editor’s built-in terminal and forgotten also stay around without any visible sign. Again, the quickest way is to check the PID and command with lsof.

Some development tools automatically switch to the next number if the specified port is in use. For example, Vite starts on a different number if its default port 5173 is taken, and shows that number in its log. Before puzzling over “my changes don’t show up” with the old number still open in the browser, look again at the URL in the startup log.

127.0.0.1 and 0.0.0.0 also clash

If you think “my server only listens on 127.0.0.1, so it shouldn’t collide with another server running on 0.0.0.0”, you will be wrong. 0.0.0.0 means “all IPv4 addresses of this machine”, and that includes 127.0.0.1. When I checked in Python, whichever was used first, the later one failed.

import socket

a = socket.socket(); a.bind(("0.0.0.0", 8001)); a.listen()
b = socket.socket()
b.bind(("127.0.0.1", 8001))   # OSError: [Errno 98] Address already in use

c = socket.socket(); c.bind(("127.0.0.1", 8002)); c.listen()
d = socket.socket()
d.bind(("0.0.0.0", 8002))     # OSError: [Errno 98] Address already in use

If the NAME column of lsof shows *:8000, the socket listens on all addresses; if it shows 127.0.0.1:8000, it listens on loopback only. Changing the address you listen on does not help as long as you use the same port number. Use a different port number, or stop the process that is already there.

No process, but the port still can’t be used: TIME_WAIT

Nothing appears in lsof or fuser, yet you get Address already in use only right after stopping the server, and after waiting about a minute it starts. In this case, a socket remains in a state called TIME_WAIT.

In TCP, the side that closes the connection first keeps information about the closed connection for a while, so that it won’t get confused if a packet that failed to arrive earlier wanders in later. This is TIME_WAIT, and on Linux it lasts 60 seconds. Because it is the kernel that holds it, it does not disappear when you terminate the process, and it is not shown in lsof. In the ss -tan list, the state column of the line for the port shows TIME-WAIT.

I reproduced it with a server of my own. The program just listens, accepts a single connection, replies, disconnects from the server side first and exits.

# srv.py
import socket, sys

reuse = len(sys.argv) > 1 and sys.argv[1] == "reuse"
s = socket.socket()
if reuse:
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("0.0.0.0", 9000))
s.listen()
conn, addr = s.accept()
conn.sendall(b"hello\n")
conn.close()      # disconnect from the server side first
s.close()

I started it, connected once from another terminal, and right after it exited started python3 srv.py again. It failed with OSError: [Errno 98] Address already in use. When I kept restarting it on the same port and timed how long it took to succeed, it was about 60 seconds.

SO_REUSEADDR is needed on “both sockets”

The socket option SO_REUSEADDR lets you start listening even while a TIME_WAIT socket remains. However, when I tried it on Linux, setting it only on the restarted server did not work. If the first run is done without it and you start python3 srv.py reuse while TIME_WAIT remains, it still fails with Errno 98.

What remains in TIME_WAIT is the socket for the connection the previous server accepted, and its settings are inherited from the previous server’s listening socket. If the previous server did not set SO_REUSEADDR, it won’t give way even if the newcomer sets it. Indeed, when I started both the first and second runs with reuse, I could start the server back to back even with TIME_WAIT remaining.

In practice, just remember “if you write a server, set SO_REUSEADDR from the start”. Many frameworks set it by default. HTTPServer, used by Python’s http.server, has allow_reuse_address enabled, so the python3 -m http.server from the start of this article does not fail because of TIME_WAIT. Node.js net / http servers could also be restarted right after being shut down with the same steps as the Python program above, and were not affected by TIME_WAIT. Programs that use socket directly, or use socketserver.TCPServer as is, have it disabled by default, so set it yourself.

Even with SO_REUSEADDR, you cannot use a port where a listening socket exists. The “forgotten servers” from the earlier sections are not solved by this option. The Linux manual page socket(7) also explains that bind is allowed unless there is an active listening socket.

Finding it on Windows

On Windows, netstat in Command Prompt shows the PID as well.

netstat -ano | findstr :8000

-a shows all connections and listening ports, -n shows numeric output, and -o shows the PID. The number at the right end of the line whose state is LISTENING is the PID. findstr matches substrings, so searching for :80 also shows lines for :8080. Check the port number in the displayed lines with your own eyes. To look up the program name from the PID and stop it, do the following.

tasklist /FI "PID eq 1234"
taskkill /PID 1234

In PowerShell, OwningProcess in Get-NetTCPConnection -LocalPort 8000 -State Listen is the PID, and Get-Process -Id <PID> tells you the program name. The error number on Windows is 10048 (WSAEADDRINUSE).

On Windows, you may also get an error even though no program is using the port. In environments where Hyper-V, WSL2 or Docker Desktop is enabled, the OS may reserve ranges of ports in bulk, and trying to listen on a port in such a range gives an access denied error (10013, WSAEACCES). You can list the reserved ranges with the following command.

netsh interface ipv4 show excludedportrange protocol=tcp

If the port you want to use is in one of these ranges, the quickest fix is to switch to a port outside them.

Finding it on macOS

macOS also has lsof, so you can check with lsof -nP -iTCP:8000 -sTCP:LISTEN as on Linux. Unlike Linux, the error number is 48 ([Errno 48] Address already in use).

If Flask won’t start on its default port 5000 and lsof shows ControlCenter, the AirPlay Receiver in macOS Monterey and later is listening on ports 5000 and 7000. Change your development server’s port to 5001 or similar, or turn off [AirPlay Receiver] in [System Settings] > [General] > [AirDrop & Handoff].

Order for narrowing down the cause

When Address already in use appears, checking in the following order is quickest.

  1. Look for the listening process with lsof -i :<port> -sTCP:LISTEN -P -n (netstat -ano on Windows).
  2. If you find one, check its command with ps. If STAT is T, it is suspended, so bring it back with fg and stop it, or kill it.
  3. If nothing is found, check again with sudo. If still nothing, think back to whether you had just stopped it. If so, it is TIME_WAIT: wait a minute, or set SO_REUSEADDR if it is your own server.
  4. If it comes back after you stop it, stop whatever manages it from outside, such as systemd or a container.

Besides ports, lsof is also useful for finding “files that were deleted but are still open”. I described how to investigate disk space that isn’t freed in Deleted a file with rm but disk space wasn’t freed (in Japanese).

Reproducing it yourself

You can reproduce the output in this article with the following commands. On Linux with Python 3 and lsof installed, no special preparation is needed.

# 1. Cause a fight over the port
python3 -m http.server 8000 >/dev/null 2>&1 &
python3 -m http.server 8000          # fails with Errno 98
lsof -i :8000 -sTCP:LISTEN -P -n

# 2. Connect to a suspended server (same state as Ctrl+Z)
kill -STOP $(lsof -t -i :8000 -sTCP:LISTEN)
timeout 3 curl -s http://127.0.0.1:8000/ ; echo "exit $?"   # 124
kill -CONT $(lsof -t -i :8000 -sTCP:LISTEN)
kill $(lsof -t -i :8000 -sTCP:LISTEN)

# 3. Leave a TIME_WAIT socket behind (srv.py is the one above)
python3 srv.py &
python3 -c "import socket,time; c=socket.create_connection(('127.0.0.1',9000)); print(c.recv(10)); time.sleep(0.3); c.close()"
wait
python3 srv.py                       # fails with Errno 98 for about 60 seconds

In step 3, if you change both python3 srv.py commands to python3 srv.py reuse, the second start succeeds even with TIME_WAIT remaining.