Make sure to not leave hanging children processes if the parent is killed

This commit is contained in:
Thomas Jackson 2014-06-20 15:57:11 -07:00
parent 943476646a
commit baacda2286

View File

@ -5,6 +5,7 @@ The main entry point for salt-api
# Import python libs
import logging
import multiprocessing
import signal
# Import salt-api libs
import salt.loader
@ -18,6 +19,7 @@ class NetapiClient(object):
'''
def __init__(self, opts):
self.opts = opts
self.processes = []
def run(self):
'''
@ -27,4 +29,17 @@ class NetapiClient(object):
for fun in netapi:
if fun.endswith('.start'):
logger.info("Starting '{0}' api module".format(fun))
multiprocessing.Process(target=netapi[fun]).start()
p = multiprocessing.Process(target=netapi[fun])
p.start()
self.processes.append(p)
# make sure to kill the subprocesses if the parent is killed
signal.signal(signal.SIGTERM, self.kill_children)
def kill_children(self, *args):
'''
Kill all of the children
'''
for p in self.processes:
p.terminate()
p.join()