Hypercorn waits for one last request before shutting down
I'm not sure if this is intended behavior, but I've run into a scenario where it seems like hypercorn is unable to immediately respond to a shutdown trigger. Instead, hypercorn blocks and won't shut down until one last request comes in.
Reproduction
The code for the reproduction can be found here tdg5/hypercorn-needs-one-last-request. It is based loosely on the example code found here.
In one terminal:
git clone https://github.com/tdg5/hypercorn-needs-one-last-request.git
cd hypercorn-needs-one-last-request
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python main.py
After the server is running, notice it has not shutdown even though SHUTDOWN, BABY! has been written to the terminal, indicating that the shutdown trigger has been completed.
In another terminal or browser or whatever, hit the server one more time:
curl http://127.0.0.1:8080
and that should cause hypercorn to exit.
Is this the way it is supposed to work?
For the record, this is not an urgent problem for me. The scenarios where I'm running hypercorn are in kubernetes deployments where I can rely on health and liveness checks to eventually make a request and help the server realize it should die.
If there are any questions I can answer or context I can provide, please let me know!
I've run into this on occassion as well.
Confirmed !!
Does this still occur with the latest release?
It will as the issue here is that asyncio Events are not threadsafe. Try something like loop.call_soon_threadsafe(shutdown_event.set) to trigger the shutdown.
Please reopen if this isn't the issue.