sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.

Home Page:https://sanic.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sanic==23.6.0 app.before_server_stop is not working as expected on Windows

hsam99 opened this issue · comments

commented

Is there an existing issue for this?

  • 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 ...