sanic
sanic copied to clipboard
Sanic==23.6.0 app.before_server_stop is not working as expected on Windows
Is there an existing issue for this?
- [X] I have searched the existing issues
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
# 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()
Expected Behavior
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
How do you run Sanic?
As a script (app.run
or Sanic.serve
)
Operating System
Windows
Sanic Version
23.6.0
Additional context
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.
if __main__ == '__name__' :
app.prepare(single_process=True)
Sanic.serve_single()
any update on this? I can't properly run close function ...