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

web.run_app(): Terminate properly with signal TERM and INT #1932

Closed
cecton opened this issue May 30, 2017 · 5 comments
Closed

web.run_app(): Terminate properly with signal TERM and INT #1932

cecton opened this issue May 30, 2017 · 5 comments
Labels

Comments

@cecton
Copy link
Contributor

cecton commented May 30, 2017

Long story short

This is more or less related to #63. My issue is that web.run_app() gracefully exit when SIGINT is sent to the process but not when SIGTERM is sent.

This is because web.run_app() catches SIGINT (with KeyboardInterrupt exception) but does not override SIGTERM handling (there is no exception raised for that, one can only use signal.signal). Most of the time it is SIGTERM that is used to gracefully shutdown a process but that will actually kill the application instead of letting the requests finish properly and response. kill, Docker and system.d use SIGTERM by default.

Expected behaviour

I suppose the default expected behavior of web.run_app() is to allow the requests to be finished before terminating the process. That's what most people would think it does. The feeling is also harder since it does it already for SIGINT.

Actual behaviour

Only SIGINT triggers a graceful exit. SIGTERM does not let the running requests finish.

Steps to reproduce

  1. run this script
import asyncio
from aiohttp import web

async def handle(request):
    await asyncio.sleep(60)
    text = "Hello"
    return web.Response(text=text)

app = web.Application()
app.router.add_get('/', handle)

web.run_app(app)
  1. do a GET request on /
  2. send SIGTERM to the process
  3. you will notice the process ends before the 60 seconds and your request has been canceled

Your environment

Python 3.6, Arch Linux

@hubo1016
Copy link
Contributor

Maybe raising a customized exception in a signal handler (instead of SystemExit) is enough to solve this?

@asvetlov
Copy link
Member

Ok, run_app should accept a flag for enabling SIGTERM handling (switched on by default).
Implementation could raise a custom exception as @hubo1016 suggested.
Raising an exception derived from SystemExit is totally fine.

@cecton would you create a Pull Request?

@cecton
Copy link
Contributor Author

cecton commented May 31, 2017

Yeay 😄 thx @asvetlov

@cecton
Copy link
Contributor Author

cecton commented Jun 12, 2017

Solved by #1946

@cecton cecton closed this as completed Jun 12, 2017
@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants