Skip to content

Commit 5d49678

Browse files
committed
Fix unittest failures when stopping the server
The stopping server mechanism fails due to the wrong PID used to stop the server, the PID used is the one from the process that start the server, instead of the mysqld process itself. This patch changes the wrong behavior to always get the PID from the PID file to stop the server. In addition this patch adds new logging entries to facilitate the debug of unittest that requires to stop the server.
1 parent 5ab7cca commit 5d49678

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

tests/mysqld.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ def process_running(pid):
106106
for name, apid, _ in lines:
107107
name = name.decode('utf-8')
108108
if name == EXEC_MYSQLD and pid == int(apid):
109+
LOGGER.debug("Process %d is running.", pid)
109110
return True
111+
LOGGER.debug("Process %d not running.", pid)
110112
return False
111113

112114
# We are on a UNIX-like system
@@ -328,12 +330,11 @@ def _start_server(self):
328330
except (OSError, ValueError) as err:
329331
raise MySQLServerError(err)
330332

331-
def _stop_server(self):
333+
def _stop_server(self, pid=None):
332334
"""Stop the MySQL server"""
333-
if not self._process:
334-
return False
335+
pid = pid if pid is not None else get_pid(self._pid_file)
335336
try:
336-
process_terminate(self._process.pid)
337+
process_terminate(pid)
337338
except (OSError, ValueError) as err:
338339
raise MySQLServerError(err)
339340

@@ -646,6 +647,7 @@ def update_config(self, **kwargs):
646647
sys.exit(1)
647648

648649
def start(self, **kwargs):
650+
LOGGER.debug("Attempting to start MySQL server %s", self.name)
649651
if self.check_running():
650652
LOGGER.error("MySQL server '{name}' already running".format(
651653
name=self.name))
@@ -691,23 +693,31 @@ def stop(self):
691693
"""
692694
pid = get_pid(self._pid_file)
693695
if not pid:
696+
LOGGER.error("Process id not found, unable to stop MySQL server.")
694697
return
695-
try:
696-
if not self._stop_server():
697-
process_terminate(pid)
698-
except (MySQLServerError, OSError) as err:
699-
if self._debug is True:
700-
raise
701-
LOGGER.error("Failed stopping MySQL server '{name}': "
702-
"{error}".format(error=str(err), name=self._name))
703-
sys.exit(1)
704-
else:
705-
time.sleep(3)
698+
LOGGER.debug("Attempting to stop MySQL server %s (pid=%s).",
699+
self._name, pid)
700+
tries = 5
701+
while tries > 0:
702+
try:
703+
self._stop_server(pid)
704+
except (MySQLServerError, OSError) as err:
705+
LOGGER.error("Failed stopping MySQL server '{name}': "
706+
"{error}".format(error=str(err), name=self._name))
707+
if self._debug is True:
708+
raise
709+
sys.exit(1)
710+
else:
711+
LOGGER.debug("Waiting for MySQL server to stop...")
712+
time.sleep(1)
713+
if not self.check_running(pid):
714+
LOGGER.debug("MySQL server stopped '{name}' "
715+
"(pid={pid})".format(pid=pid, name=self._name))
716+
return True
717+
tries =- 1
706718

707-
if self.check_running(pid):
708-
LOGGER.debug("MySQL server stopped '{name}' "
709-
"(pid={pid})".format(pid=pid, name=self._name))
710-
return True
719+
LOGGER.error("Failed stopping MySQL server '{name}': "
720+
"{error}".format(error=str(err), name=self._name))
711721

712722
return False
713723

@@ -734,7 +744,7 @@ def check_running(self, pid=None):
734744
"""
735745
pid = pid or get_pid(self._pid_file)
736746
if pid:
737-
LOGGER.debug("Got PID %d", pid)
747+
LOGGER.debug("Checking PID %d", pid)
738748
return process_running(pid)
739749

740750
return False

0 commit comments

Comments
 (0)