hypercorn icon indicating copy to clipboard operation
hypercorn copied to clipboard

Hypercorn waits for one last request before shutting down

Open tdg5 opened this issue 2 years ago • 2 comments

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!

tdg5 avatar Sep 19 '23 14:09 tdg5

I've run into this on occassion as well.

XChikuX avatar Oct 29 '23 02:10 XChikuX

Confirmed !!

mrchoke avatar Nov 10 '23 07:11 mrchoke

Does this still occur with the latest release?

pgjones avatar May 28 '24 20:05 pgjones

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.

pgjones avatar May 28 '24 20:05 pgjones