quart icon indicating copy to clipboard operation
quart copied to clipboard

`Quart.after_serving`, `Quart.shutdown` does not execute / no graceful shutdowns on Windows

Open f3ndot opened this issue 2 years ago • 1 comments

Hello! When running this example code on macOS and Windows 10, macOS will gracefully shutdown, evaluating my_shutdown(), whereas Windows 10 will throw a KeyboardInterrupt exception and immediately exit without calling my_shutdown(). Additionally, I don't think it runs Quart's shutdown() method either, thereby not letting background tasks finish gracefully.

Versions

  • OS: Windows 10 21H2 (19044.1706)
  • Quart 0.17
  • Python 3.9.6, Python 3.10.4

Minimally Reproducible Example

from quart import Quart

app = Quart(__name__)


@app.before_serving
async def startup():
    print("STARTING")


@app.after_serving
async def my_shutdown():
    print("SHUTTING DOWN")


@app.route("/")
async def index():
    return "hello"


app.run()

Console Output

C:\Users\Justin\Code\quart-testing>python quart_test.py
 * Serving Quart app 'quart_test'
 * Environment: production
 * Please use an ASGI server (e.g. Hypercorn) directly in production
 * Debug mode: False
 * Running on http://127.0.0.1:5000 (CTRL + C to quit)
STARTING
[2022-06-05 14:03:54,729] Running on http://127.0.0.1:5000 (CTRL + C to quit)
Traceback (most recent call last):
  File "C:\Users\Justin\Code\quart-testing\quart_test.py", line 21, in <module>
    app.run()
  File "C:\Users\Justin\Code\quart-testing\.venv\lib\site-packages\quart\app.py", line 1270, in run
    loop.run_until_complete(task)
  File "C:\Users\Justin\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 629, in run_until_complete
    self.run_forever()
  File "C:\Users\Justin\AppData\Local\Programs\Python\Python39\lib\asyncio\windows_events.py", line 316, in run_forever
    super().run_forever()
  File "C:\Users\Justin\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 596, in run_forever
    self._run_once()
  File "C:\Users\Justin\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 1854, in _run_once
    event_list = self._selector.select(timeout)
  File "C:\Users\Justin\AppData\Local\Programs\Python\Python39\lib\asyncio\windows_events.py", line 434, in select
    self._poll(timeout)
  File "C:\Users\Justin\AppData\Local\Programs\Python\Python39\lib\asyncio\windows_events.py", line 783, in _poll
    status = _overlapped.GetQueuedCompletionStatus(self._iocp, ms)
KeyboardInterrupt
^C

f3ndot avatar Jun 05 '22 18:06 f3ndot

A lead: I've learned that the reloader that fires when source code is changed and saved will indeed execute shutdown appropriately before restarting Quart.

f3ndot avatar Jun 05 '22 18:06 f3ndot

This is fixed in a84882232a62dfd676d51529f1e9b73db388d9d4

pgjones avatar Oct 03 '22 20:10 pgjones