AI assistant runs two HTTP servers on the same port
An AI assistant served an HTML preview, but edits never showed up. Two HTTP servers were bound to the same port: the browser talked to the old one while the AI edited files the new one read. On Windows, SO_REUSEADDR lets a second process bind the same port silently, and Python's http.server enables it by default. Includes diagnosis commands, a safe launcher, and a verification rule for AI edits.
📜 Table of Contents
📖 Keep reading
Two HTTP servers on one port: why AI edits never took effect
I asked an AI assistant for an HTML preview of a video script. It wrote the page and started a server: Serving HTTP on 0.0.0.0 port 8000.
The page loaded. I asked for a larger headline and a dark blue palette. It edited the file and said "updated, just refresh."
Refresh. No change. Hard refresh. No change. We spent forty minutes blaming browser cache and CSS specificity. cat showed the edit was in the file.
The port had two server processes. The browser was talking to the first; the AI was editing files the second one served.
1. Why Windows makes this easy
On Linux the second bind fails loudly:
OSError: [Errno 98] Address already in use
On Windows, SO_REUSEADDR historically means "allow binding an address that is already in use", not just handling TIME_WAIT as on Unix. Two processes can both bind 0.0.0.0:8000, and which one accepts a connection is up to the scheduler.
Python's http.server.HTTPServer sets allow_reuse_address = 1 by default, so:
python -m http.server 8000 # first, serving the browser
python -m http.server 8000 # second, no error, prints "Serving HTTP on ..."
In my case the two processes also had different working directories, so "editing the file" meant editing a file the browser never fetched.
2. Three commands to confirm
netstat -ano | findstr :8000 # Windows: two LISTENING rows with different PIDs
tasklist /FI "PID eq 12345"
taskkill /F /PID 12345
ss -ltnp | grep :8000 # Linux
lsof -iTCP:8000 -sTCP:LISTEN -P -n
Check what the browser actually receives:
curl -s -o /dev/null -w "%{http_code} %{size_download}\n" http://localhost:8000/
Run it several times. Changing sizes mean two processes are answering in turn.
3. A launcher that refuses to lie
import socket, sys, os, http.server, socketserver
PORT, HOST = 8000, "127.0.0.1"
def in_use(host, port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(0.5)
return s.connect_ex((host, port)) == 0
if in_use(HOST, PORT):
sys.exit(f"port {PORT} in use; check netstat -ano | findstr :{PORT}")
class Handler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header("Cache-Control", "no-store")
super().end_headers()
class Server(socketserver.ThreadingTCPServer):
allow_reuse_address = False # critical on Windows
daemon_threads = True
with Server((HOST, PORT), Handler) as httpd:
print(f"serving on http://{HOST}:{PORT} pid={os.getpid()}")
httpd.serve_forever()
Printing the PID means netstat output maps directly to a process. no-store removes cache as a suspect.
4. Make the AI prove it
"Updated" is a claim, not a verification. Require two checks:
grepthe edited file for the new stringcurlthe served page and grep again
curl -s http://localhost:8000/ | grep -c "dark-blue"
Zero means the server is not serving that file. Then the answer is "changed but not live, investigating" rather than "updated".
5. One entry point, one port table
| Project | Port | Directory |
|---|---|---|
| deck preview | 8010 | C:/proj/deck |
| site debug | 8020 | C:/proj/site/dist |
| mock API | 8030 | C:/proj/mock |
Front them with caddy or nginx so "it won't open" has one place to look.
6. Symptom table
| Symptom | Cause | Check |
|---|---|---|
| edits ignored | two processes on the port | netstat -ano |
| edits ignored | wrong working directory | curl and grep |
| edits ignored | service worker | DevTools → unregister |
| edits ignored | stale build output | timestamps in dist/ |
| intermittent | two processes alternating | five curls compared |
FAQ
Why no "port in use" error on Windows? SO_REUSEADDR semantics plus Python's default. The same code errors on Linux.
The process is missing from Task Manager. git-bash may run it in another session; find the PID via netstat and look it up with tasklist.
localhost vs 127.0.0.1? localhost may resolve to ::1 while the server listens on IPv4 only. Test both.
How do I know the AI really restarted it? Have it print the new PID and start time, or expose a /healthz that returns the start timestamp.
Platform-specific SO_REUSEADDR behaviour varies; check your Python distribution's socketserver source and the official docs.
Related
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.