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

Fix watchdog reload worker repeatedly if there are multiple changed files #1555

Closed
wants to merge 8 commits into from
19 changes: 11 additions & 8 deletions sanic/reloader_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def kill_process_children(pid):
pass # should signal error here


def kill_program_completly(proc):
def kill_program_completely(proc):
"""Kill worker and it's child processes and exit.

:param proc: worker process (process ID)
Expand All @@ -141,12 +141,14 @@ def watchdog(sleep_interval):
mtimes = {}
worker_process = restart_with_reloader()
signal.signal(
signal.SIGTERM, lambda *args: kill_program_completly(worker_process)
signal.SIGTERM, lambda *args: kill_program_completely(worker_process)
)
signal.signal(
signal.SIGINT, lambda *args: kill_program_completly(worker_process)
signal.SIGINT, lambda *args: kill_program_completely(worker_process)
)
while True:
need_reload = False

for filename in _iter_module_files():
try:
mtime = os.stat(filename).st_mtime
Expand All @@ -156,12 +158,13 @@ def watchdog(sleep_interval):
old_time = mtimes.get(filename)
if old_time is None:
mtimes[filename] = mtime
continue
elif mtime > old_time:
kill_process_children(worker_process.pid)
worker_process.terminate()
worker_process = restart_with_reloader()
mtimes[filename] = mtime
break
need_reload = True

if need_reload:
kill_process_children(worker_process.pid)
worker_process.terminate()
worker_process = restart_with_reloader()

sleep(sleep_interval)