Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
hsam99 opened this issue Nov 27, 2023 · 1 comment
Open
1 task done
Labels

Comments

@hsam99
Copy link

hsam99 commented Nov 27, 2023

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()
@hsam99 hsam99 added the bug label Nov 27, 2023
@VerifyBot
Copy link

any update on this? I can't properly run close function ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants