Open hsam99 opened 8 months ago
In Sanic==23.6.0, the handler for app.before_server_stop is not invoked when Ctrl+c is pressed on Windows. The same code works as expected on Linux (i.e. app.before_server_stop handler is invoked when Ctrl+c is pressed).
# main.py from sanic import Sanic app = Sanic('test_app') @app.before_server_start async def listener_1(_app): print("123") @app.before_server_stop async def listener_2(_app): print("456") @app.route('/') async def hello(request): return Sanic.response.text('123') if __name__ == '__main__': app.run()
When Ctrl+c is pressed, 456 should be printed on the screen. But I got the following output:
python main.py [2023-11-24 17:24:50 +0000] [26412] [WARNING] Sanic is running in PRODUCTION mode. Consider using '--debug' or '--dev' while actively developing your application. [2023-11-24 17:24:51 +0000] [17912] [INFO] Sanic Extensions: [2023-11-24 17:24:51 +0000] [17912] [INFO] > injection [0 dependencies; 0 constants] [2023-11-24 17:24:51 +0000] [17912] [INFO] > openapi [http://127.0.0.1:8000/docs] [2023-11-24 17:24:51 +0000] [17912] [INFO] > http [2023-11-24 17:24:51 +0000] [17912] [INFO] > templating [jinja2==3.1.2] 123 [2023-11-24 17:24:51 +0000] [17912] [INFO] Starting worker [17912] [2023-11-24 17:24:52 +0000] [26412] [INFO] Received signal SIGINT. Shutting down. [2023-11-24 17:24:52 +0000] [26412] [INFO] Server Stopped
As a script (app.run or Sanic.serve)
app.run
Sanic.serve
Windows
23.6.0
Workaround is to set single_process=True and use Sanic.serve_single() to run the server. This way "456" is printed when Ctrl+c is pressed on Windows.
single_process=True
Sanic.serve_single()
if __main__ == '__name__' : app.prepare(single_process=True) Sanic.serve_single()
any update on this? I can't properly run close function ...
Is there an existing issue for this?
Describe the bug
In Sanic==23.6.0, the handler for app.before_server_stop is not invoked when Ctrl+c is pressed on Windows. The same code works as expected on Linux (i.e. app.before_server_stop handler is invoked when Ctrl+c is pressed).
Code snippet
Expected Behavior
When Ctrl+c is pressed, 456 should be printed on the screen. But I got the following output:
How do you run Sanic?
As a script (
app.run
orSanic.serve
)Operating System
Windows
Sanic Version
23.6.0
Additional context
Workaround is to set
single_process=True
and useSanic.serve_single()
to run the server. This way "456" is printed when Ctrl+c is pressed on Windows.