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: Handle SIGTERM more gracefully in watchmedo #693

Merged
merged 2 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ as command-line arguments and logs events generated:
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
finally:
observer.stop()
observer.join()
observer.join()
Shell Utilities
Expand Down
3 changes: 2 additions & 1 deletion changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Changelog
- Replace mutable default arguments with ``if None`` implementation (`#677 <https://github.com/gorakhargosh/watchdog/pull/677>`_)
- Expand tests to Python 2.7 and 3.5-3.10 for GNU/Linux, macOS and Windows
- [mac] Performance improvements for the `fsevents` module (`#680 <https://github.com/gorakhargosh/watchdog/pull/680>`_)
- Thanks to our beloved contributors: @Sraw, @CCP-Aporia, @BoboTiG
- Handle shutdown events from SIGTERM and SIGINT to `watchmedo` more reliably (`#693 <https://github.com/gorakhargosh/watchdog/pull/693>`_)
- Thanks to our beloved contributors: @Sraw, @CCP-Aporia, @BoboTiG, @maybe-sybr


0.10.3
Expand Down
4 changes: 2 additions & 2 deletions docs/source/examples/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
finally:
observer.stop()
observer.join()
observer.join()
4 changes: 2 additions & 2 deletions docs/source/examples/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ def on_any_event(self, event):
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
finally:
observer.stop()
observer.join()
observer.join()
4 changes: 2 additions & 2 deletions docs/source/examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ def on_modified(self, event):
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
finally:
observer.stop()
observer.join()
observer.join()
4 changes: 2 additions & 2 deletions docs/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ file system changes and simply log them to the console::
try:
while observer.isAlive():
observer.join(1)
except KeyboardInterrupt:
finally:
observer.stop()
observer.join()
observer.join()

To stop the program, press Control-C.
7 changes: 7 additions & 0 deletions src/watchdog/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ class UnsupportedLibc(Exception):
pass


class WatchdogShutdown(Exception):
"""
Semantic exception used to signal an external shutdown event.
"""
pass


class BaseThread(threading.Thread):
""" Convenience class for creating stoppable threads. """

Expand Down
30 changes: 20 additions & 10 deletions src/watchdog/watchmedo.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

from argh import arg, aliases, ArghParser, expects_obj
from watchdog.version import VERSION_STRING
from watchdog.utils import load_class
from watchdog.utils import WatchdogShutdown, load_class


logging.basicConfig(level=logging.INFO)
Expand Down Expand Up @@ -117,7 +117,7 @@ def observe_with(observer, event_handler, pathnames, recursive):
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
except WatchdogShutdown:
observer.stop()
observer.join()

Expand Down Expand Up @@ -198,7 +198,7 @@ def tricks_from(args):
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
except WatchdogShutdown:
for o in observers:
o.unschedule_all()
o.stop()
Expand Down Expand Up @@ -531,12 +531,18 @@ def auto_restart(args):
else:
stop_signal = int(args.signal)

# Handle SIGTERM in the same manner as SIGINT so that
# this program has a chance to stop the child process.
def handle_sigterm(_signum, _frame):
raise KeyboardInterrupt()
# Handle termination signals by raising a semantic exception which will
# allow us to gracefully unwind and stop the observer
termination_signals = {signal.SIGTERM, signal.SIGINT}

signal.signal(signal.SIGTERM, handle_sigterm)
def handler_termination_signal(_signum, _frame):
# Neuter all signals so that we don't attempt a double shutdown
for signum in termination_signals:
signal.signal(signum, signal.SIG_IGN)
raise WatchdogShutdown

for signum in termination_signals:
signal.signal(signum, handler_termination_signal)

patterns, ignore_patterns = parse_patterns(args.patterns,
args.ignore_patterns)
Expand All @@ -550,8 +556,12 @@ def handle_sigterm(_signum, _frame):
kill_after=args.kill_after)
handler.start()
observer = Observer(timeout=args.timeout)
observe_with(observer, handler, args.directories, args.recursive)
handler.stop()
try:
observe_with(observer, handler, args.directories, args.recursive)
except WatchdogShutdown:
pass
finally:
handler.stop()


epilog = """Copyright 2011 Yesudeep Mangalapilly <yesudeep@gmail.com>.
Expand Down