From 657a6ed262f1e9611e5459b6f33aaf09c10e377b Mon Sep 17 00:00:00 2001 From: Fabio Erculiani Date: Sun, 18 Apr 2010 18:49:46 +0200 Subject: [PATCH] [molecule] kill all running children pids on exit --- molecule.py | 9 +++++++++ molecule/utils.py | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/molecule.py b/molecule.py index af19be1..fbba4e0 100644 --- a/molecule.py +++ b/molecule.py @@ -17,12 +17,19 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +import os +import signal import sys sys.path.insert(0,'/usr/lib/molecule/') sys.path.insert(0,'molecule/') sys.path.insert(0,'.') import molecule.cmdline from molecule.handlers import Runner +from molecule.utils import RUNNING_PIDS + +def kill_pids(): + for pid in RUNNING_PIDS: + os.kill(pid, signal.SIGTERM) molecule_data, molecule_data_order = molecule.cmdline.parse() if not molecule_data_order: @@ -35,8 +42,10 @@ for el in molecule_data_order: rc = my.run() except KeyboardInterrupt: my.kill() + kill_pids() raise SystemExit(1) my.kill() + kill_pids() if rc != 0: raise SystemExit(rc) raise SystemExit(0) diff --git a/molecule/utils.py b/molecule/utils.py index dc14031..4f27c2c 100644 --- a/molecule/utils.py +++ b/molecule/utils.py @@ -24,6 +24,8 @@ import time import subprocess import shutil +RUNNING_PIDS = set() + def get_year(): return time.strftime("%Y") @@ -58,7 +60,9 @@ def exec_chroot_cmd(args, chroot, pre_chroot = []): rc = subprocess.call(myargs) os._exit(rc) else: + RUNNING_PIDS.add(pid) rcpid, rc = os.waitpid(pid,0) + RUNNING_PIDS.discard(pid) return rc def empty_dir(dest_dir):