·4 min read

Python http.server Errno 48: address in use

A preview tool said the dev server exited with code 1 and a long Python traceback. The last line, Errno 48, meant a copy was already running and fine.

workflowclaude-codeweb

Contents

A preview tool in our editor told us the dev server had failed:

The dev server exited during startup (code 1). Fix the error in the output below,
then start the server again.

Under it came a Python traceback of about 30 lines, in color, through runpy, http/server.py and socketserver.py. It looks like a crash in Python itself. It was not. The server was fine, and the only thing wrong was that we had tried to start it twice.

If you searched for python http.server OSError: [Errno 48] Address already in use, this is the short answer, and how to confirm it in under a minute.

Read the last line, not the first

The traceback went down through these frames:

File ".../http/server.py", line 1323, in <module>
    test(
File ".../http/server.py", line 1270, in test
    with ServerClass(addr, HandlerClass) as httpd:
File ".../socketserver.py", line 457, in __init__
    self.server_bind()
File ".../http/server.py", line 1317, in server_bind
    return super().server_bind()
File ".../socketserver.py", line 478, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 48] Address already in use

Python prints the call chain first and the actual error last. Here every frame is the standard library walking from python -m http.server down to one call, socket.bind, and that call failed. Errno 48 on macOS means the address you asked for is taken. On Linux the same error is number 98. Either way the message is the same: another process is already listening on that port.

Nothing in the traceback points at your code. That is the quickest check: if every frame is inside the standard library, the cause is almost always the environment, not a bug.

Find who has the port

We asked the system which process held port 5174:

lsof -nP -iTCP:5174 -sTCP:LISTEN

That prints the process id and the command. To see when it started, pass the id to ps:

lsof -nP -iTCP:5174 -sTCP:LISTEN -t | xargs -I{} ps -o pid,lstart,command -p {}

The result was a Python file server, the same command we had just tried to start, running since 16:13, which was before the preview tool's attempt. So the tool launched a second copy, and the second copy could not take the port.

Check that the old one works

One more step before deciding anything:

curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:5174/

It printed a normal success code, and the page body looked right. The existing server answered. We opened the preview on it and the game loaded on its title screen, with its credits shown.

What we did not do

We did not kill the process. The obvious reaction to "address already in use" is to find the process id and kill it, then start again. We left it alone for two reasons.

First, it was the same server we wanted. Killing a working copy to start an identical one gains nothing.

Second, we could not tell who owned it. Another terminal or another coding session on the same machine may have started it, and may be using it right now. In our case a re-import job was still running in the background and rewriting the files that server was serving. Killing the server would have broken a page someone else had open.

If you are sure the process is yours and is stuck, kill it. If you are not sure, leave it, use it, and say so.

Three ways to avoid it

Reuse the running server. If the port answers and serves the right folder, you do not need a new one. This is what we did.

Pick another port. python3 -m http.server 5175 starts a second server beside the first. This is the right move when the running one serves a different folder than the one you need.

Let the tool check first. A preview tool or a script that starts servers can test the port before it launches. If something answers, it should attach to it rather than start another process and report a failure when that fails. Ours reported "exited during startup" for a case that was healthy, and that message was what sent us reading a traceback.

A note on the folder

One trap in the "reuse it" case: a file server serves the folder it was started in. If the running copy was started in a different project, it will answer with a 200 and the wrong files. Our check passed because the page we asked for was there and showed the right game. Request something that only your project has, not just the root, before you trust a running server.

Summary

  • A traceback that ends in OSError: [Errno 48] Address already in use means the port is taken.
  • lsof -nP -iTCP:<port> -sTCP:LISTEN tells you by what and since when.
  • curl tells you whether it works.
  • If it works and you did not start it on purpose, use it. Do not kill what you cannot account for.

The long traceback made a small thing look serious. The error was on the last line the whole time.

More posts

New writing when there is something worth saying. By email or by RSS, whichever you prefer.