-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Graceful shutdown sequence doesn't terminate the existing TCP connections gracefully #3638
Comments
GitMate.io thinks the contributor most likely able to help you is @asvetlov. Possibly related issues are #1369 (Graceful shutdown can (I think) truncate connections that it shouldn't), #1486 (non-working example of the documentation graceful-shutdown - KeyError: websockets), #183 (Streaming uploads doesn't seem to work), #3075 (trust_env doesn't works), and #2298 (graceful shutdown or reload with gunicorn without shutdown connections). |
does not work 2 |
You did something wrong |
@dwc147896325 You did nothing wrong. For anybody having this issue, revert your aiohttp version to 3.4.4 which doesn't suffer from this bug or apply this fix I've put together for the latest version: amitbl@8d42315 |
Do you use Windows? |
Yes, I use windows
On Mon, May 27, 2019 at 6:29 PM Andrew Svetlov ***@***.***> wrote:
Do you use Windows?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3638?email_source=notifications&email_token=AC3AQ7HUMUGA4AJLVRLRU2DPXROF5A5CNFSM4G373CSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWKSV6A#issuecomment-496315128>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AC3AQ7F7HGVL5BKPG7JOBODPXROF5ANCNFSM4G373CSA>
.
--
Johnathan
|
@asvetlov Another case, this fix solve problem of Graceful shutdown with healthchecks (Consul, Docker, k8s). |
Yea, it would be nice to run Also, the logic seems to be a little bit jumbled. As I see, when Such a fix would enable graceful shutdown and, for example, rolling updates on k8s. What do you think? @asvetlov |
I tried to implement a graceful shutdown using documentation, but it does not work, just like it is described in the issue. import asyncio
from aiohttp import web
import os
import signal
from threading import Thread
import time
def _on_shutdown(pid):
time.sleep(6)
# Sending SIGINT to close server
os.kill(pid, signal.SIGINT)
async def _graceful_shutdown_ctx(app):
def graceful_shutdown_sigterm_handler():
nonlocal thread
thread = Thread(target=_on_shutdown, args=(os.getpid(),))
thread.start()
thread = None
loop = asyncio.get_event_loop()
loop.add_signal_handler(
signal.SIGTERM, graceful_shutdown_sigterm_handler,
)
yield
loop.remove_signal_handler(signal.SIGTERM)
if thread is not None:
thread.join()
async def _handler(request):
await asyncio.sleep(5)
return web.Response(text='yes')
def run():
app = web.Application()
app.router.add_get('/', _handler)
app.cleanup_ctx.append(_graceful_shutdown_ctx)
web.run_app(app, handle_signals=False)
if __name__ == '__main__':
run() |
@KonradSwierczynski it works on the server side if you set |
Another workaround for it. GracefulExit can be used to trigger original signal_handler functinality. To be correct it's also good idea not accept new connections during shutdown hook is running (and probably closing existing websocket connections in common use case) import os
import asyncio
from aiohttp.web_runner import GracefulExit
async def shutdown_hook(app, sig):
print("shutdown_hook, implement your graceful close here")
# just sleep as placeholder
await asyncio.sleep(1)
print(".")
await asyncio.sleep(1)
print("...")
# eventually retrigger signal to deliver GracefulExit to server
os.kill(os.getpid(), sig)
async def app_ctx(app):
# start up init here
shutdown = False
def signal_handler(sig):
nonlocal shutdown
if shutdown:
raise GracefulExit()
else:
shutdown = True # flag can be also used to stop accepting new websocket connections
asyncio.create_task(shutdown_hook(app, sig))
asyncio.get_event_loop().add_signal_handler(signal.SIGINT, signal_handler, signal.SIGINT)
asyncio.get_event_loop().add_signal_handler(signal.SIGTERM, signal_handler, signal.SIGTERM)
yield
# cleanup here ..
app.cleanup_ctx.append(app_ctx) |
I couldn't reproduce this in v3.8.3. |
This was resolved a while ago and improved further in 3.9, with the shutdown steps documented: |
Long story short
server code as below:
Expected behaviour
graceful shutdown should terminate the request processing before stopping the server process, canceling the existing requests processing coroutines.
Steps to reproduce
SIGTERM
orSIGINT
to the server processaiohttp.client_exceptions.ServerDisconnectedError
before the server shutdownYour environment
Python 3.7.1, Centos 7.2.1511, aiohttp 3.5.4
The text was updated successfully, but these errors were encountered: