Skip to content

Commit

Permalink
Ensure CrateDB process gets terminated on cr8 sigterm
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mfussenegger committed Apr 8, 2024
1 parent 7531dde commit bd05670
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions cr8/run_crate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import socket
import ssl
import platform
import signal
from datetime import datetime
from hashlib import sha1
from pathlib import Path
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit bd05670

Please sign in to comment.