pgjones / hypercorn

Hypercorn is an ASGI and WSGI Server based on Hyper libraries and inspired by Gunicorn.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Running a second server on the same port doesn't fail on Windows

vxgmichel opened this issue · comments

I noticed that starting the same server twice doesn't fail on Windows.

Here's the server I used to run the test:

import os
import asyncio
from quart import Quart
from hypercorn.asyncio import serve
from hypercorn import Config
app = Quart(__name__)

@app.route('/')
async def hello():
    return f"{os.getpid()}"

if __name__ == "__main__":
    config = Config()
    asyncio.run(serve(app, config))

This is what happens on Linux:

$ python test_app.py 
[2023-12-08 14:02:04 +0100] [41578] [INFO] Running on http://127.0.0.1:8000 (CTRL + C to quit)
# Simultaneously
$ python test_app.py 
[...]
OSError: [Errno 98] Address already in use

And what happens on Windows:

$ python test_app.py
[2023-12-08 15:01:21 +0100] [2964] [INFO] Running on http://127.0.0.1:8000 (CTRL + C to quit)
$ python test_app.py
[2023-12-08 15:01:53 +0100] [6916] [INFO] Running on http://127.0.0.1:8000 (CTRL + C to quit)

Using a curl localhost:8000 shows that the first server answers the request, until it's killed. Then the next curl requests are answered by the second server.

I ran a quick investigation and it seems related to this line, which is executed both on Windows and Linux:

sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

Maybe this should be Linux only?

Why should it fail?