1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-25 01:38:08 +00:00

Kill VPCS process when the server exit

This commit is contained in:
Julien Duponchelle 2015-01-20 16:24:46 +01:00
parent 531265eced
commit 7cf409c392

View File

@ -28,6 +28,7 @@ import re
import asyncio import asyncio
import socket import socket
import shutil import shutil
import atexit
from pkg_resources import parse_version from pkg_resources import parse_version
from .vpcs_error import VPCSError from .vpcs_error import VPCSError
@ -81,6 +82,11 @@ class VPCSVM(BaseVM):
self._check_requirements() self._check_requirements()
def __del__(self):
self._kill_process()
def _check_requirements(self): def _check_requirements(self):
""" """
Check if VPCS is available with the correct version Check if VPCS is available with the correct version
@ -166,9 +172,9 @@ class VPCSVM(BaseVM):
self._command = self._build_command() self._command = self._build_command()
try: try:
log.info("starting VPCS: {}".format(self._command)) log.info("Starting VPCS: {}".format(self._command))
self._vpcs_stdout_file = os.path.join(self.working_dir, "vpcs.log") self._vpcs_stdout_file = os.path.join(self.working_dir, "vpcs.log")
log.info("logging to {}".format(self._vpcs_stdout_file)) log.info("Logging to {}".format(self._vpcs_stdout_file))
flags = 0 flags = 0
if sys.platform.startswith("win32"): if sys.platform.startswith("win32"):
flags = subprocess.CREATE_NEW_PROCESS_GROUP flags = subprocess.CREATE_NEW_PROCESS_GROUP
@ -178,12 +184,14 @@ class VPCSVM(BaseVM):
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
cwd=self.working_dir, cwd=self.working_dir,
creationflags=flags) creationflags=flags)
#atexit(self._kill_process) # Ensure we don't leave orphan process
log.info("VPCS instance {} started PID={}".format(self.name, self._process.pid)) log.info("VPCS instance {} started PID={}".format(self.name, self._process.pid))
self._started = True self._started = True
except (OSError, subprocess.SubprocessError) as e: except (OSError, subprocess.SubprocessError) as e:
vpcs_stdout = self.read_vpcs_stdout() vpcs_stdout = self.read_vpcs_stdout()
log.error("could not start VPCS {}: {}\n{}".format(self._path, e, vpcs_stdout)) log.error("Could not start VPCS {}: {}\n{}".format(self._path, e, vpcs_stdout))
raise VPCSError("could not start VPCS {}: {}\n{}".format(self._path, e, vpcs_stdout)) raise VPCSError("Could not start VPCS {}: {}\n{}".format(self._path, e, vpcs_stdout))
@asyncio.coroutine @asyncio.coroutine
def stop(self): def stop(self):
@ -193,17 +201,27 @@ class VPCSVM(BaseVM):
# stop the VPCS process # stop the VPCS process
if self.is_running(): if self.is_running():
log.info("stopping VPCS instance {} PID={}".format(self.name, self._process.pid)) self._kill_process()
if sys.platform.startswith("win32"):
self._process.send_signal(signal.CTRL_BREAK_EVENT)
else:
self._process.terminate()
yield from self._process.wait() yield from self._process.wait()
self._process = None self._process = None
self._started = False self._started = False
def _kill_process(self):
"""Kill the process if running"""
if self._process:
log.info("Stopping VPCS instance {} PID={}".format(self.name, self._process.pid))
if sys.platform.startswith("win32"):
self._process.send_signal(signal.CTRL_BREAK_EVENT)
else:
try:
self._process.terminate()
# Sometime the process can already be dead when we garbage collect
except ProcessLookupError:
pass
def read_vpcs_stdout(self): def read_vpcs_stdout(self):
""" """
Reads the standard output of the VPCS process. Reads the standard output of the VPCS process.