From 6b158e79ac3f0cc50226896b0239f2b95f396371 Mon Sep 17 00:00:00 2001 From: Mathias Fussenegger Date: Mon, 8 Apr 2024 11:13:16 +0200 Subject: [PATCH] Ensure CrateDB process gets terminated on cr8 sigterm The CrateDB process leaked if `cr8 run-crate` was killed via a SIGTERM. For example, in a bash script like: cr8 run-crate latest-nightly & N1=$! while ! crash --hosts localhost:4200 -c "select 1" > /dev/null 2>&1; do sleep 1 done jps kill $N1 jps --- cr8/run_crate.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/cr8/run_crate.py b/cr8/run_crate.py index b6d426a..8f2ab9b 100644 --- a/cr8/run_crate.py +++ b/cr8/run_crate.py @@ -18,6 +18,7 @@ import socket import ssl import platform +import signal from datetime import datetime from hashlib import sha1 from pathlib import Path @@ -276,7 +277,7 @@ def __init__(self, raise SystemExit('Your locale are not configured correctly. ' 'Please set LANG or alternatively LC_ALL.') self.monitor = OutputMonitor() - self.process = None # type: Optional[subprocess.Popen] + self.process : Optional[subprocess.Popen] = None self.http_url = None # type: Optional[str] self.http_host = None # type: Optional[str] start_script = 'crate.bat' if sys.platform == 'win32' else 'crate' @@ -379,7 +380,11 @@ def _set_addr(self, protocol, addr): def stop(self): if self.process: self.process.terminate() - self.process.communicate(timeout=120) + try: + self.process.communicate(timeout=120) + except subprocess.TimeoutExpired: + self.process.kill() + self.process.communicate() self.addresses = DotDict({}) self.http_host = None self.http_url = None @@ -790,6 +795,13 @@ def run_crate( keep_data, java_magic=not disable_java_magic, ) as n: + + def stop(signum, frame): + if n.process: + n.process.send_signal(signum) + sys.exit(signum) + + signal.signal(signal.SIGTERM, stop) try: n.start() n.process.wait()