Skip to content

Commit 8c193b2

Browse files
authored
Add force node stopping using SIGKILL in case of unsuccessful pg_ctl stop (#139)
1 parent e1ed158 commit 8c193b2

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

Diff for: testgres/node.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,9 @@ def version(self):
330330
"""
331331
return self._pg_version
332332

333-
def _try_shutdown(self, max_attempts):
333+
def _try_shutdown(self, max_attempts, with_force=False):
334334
attempts = 0
335+
node_pid = self.pid
335336

336337
# try stopping server N times
337338
while attempts < max_attempts:
@@ -341,12 +342,30 @@ def _try_shutdown(self, max_attempts):
341342
except ExecUtilException:
342343
pass # one more time
343344
except Exception:
344-
# TODO: probably should kill stray instance
345345
eprint('cannot stop node {}'.format(self.name))
346346
break
347347

348348
attempts += 1
349349

350+
# If force stopping is enabled and PID is valid
351+
if with_force and node_pid != 0:
352+
# If we couldn't stop the node
353+
p_status_output = self.os_ops.exec_command(cmd=f'ps -p {node_pid}', shell=True).decode('utf-8')
354+
if self.status() != NodeStatus.Stopped and p_status_output and str(node_pid) in p_status_output:
355+
try:
356+
eprint(f'Force stopping node {self.name} with PID {node_pid}')
357+
self.os_ops.kill(node_pid, signal.SIGKILL, expect_error=False)
358+
except Exception:
359+
# The node has already stopped
360+
pass
361+
362+
# Check that node stopped
363+
p_status_output = self.os_ops.exec_command(f'ps -p {node_pid}', shell=True, expect_error=True).decode('utf-8')
364+
if p_status_output and str(node_pid) in p_status_output:
365+
eprint(f'Failed to stop node {self.name}.')
366+
else:
367+
eprint(f'Node {self.name} has been stopped successfully.')
368+
350369
def _assign_master(self, master):
351370
"""NOTE: this is a private method!"""
352371

Diff for: testgres/operations/local_ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,10 @@ def remove_file(self, filename):
293293
return os.remove(filename)
294294

295295
# Processes control
296-
def kill(self, pid, signal):
296+
def kill(self, pid, signal, expect_error=False):
297297
# Kill the process
298298
cmd = "kill -{} {}".format(signal, pid)
299-
return self.exec_command(cmd)
299+
return self.exec_command(cmd, expect_error=expect_error)
300300

301301
def get_pid(self):
302302
# Get current process id

0 commit comments

Comments
 (0)