From 6993f5c88678689240222dcc07d8d6201c0c5892 Mon Sep 17 00:00:00 2001 From: Jim Paris Date: Sat, 3 Aug 2013 11:13:30 -0400 Subject: [PATCH] Fix process termination in nilm-pipewatch --- nilmtools/pipewatch.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/nilmtools/pipewatch.py b/nilmtools/pipewatch.py index 65c88d2..38885a5 100755 --- a/nilmtools/pipewatch.py +++ b/nilmtools/pipewatch.py @@ -84,13 +84,16 @@ def pipewatch(args): bufsize = -1, close_fds = True, stdin = devnull, stdout = subprocess.PIPE, - stderr = None) + stderr = None, + preexec_fn = os.setpgrp) consumer = subprocess.Popen(args.consumer, shell = True, bufsize = -11, close_fds = True, stdin = subprocess.PIPE, - stdout = None, stderr = None) + stdout = None, + stderr = None, + preexec_fn = os.setpgrp) - queue = Queue.Queue(maxsize = 32) + queue = Queue.Queue(maxsize = 4) reader = threading.Thread(target = reader_thread, args = (queue, generator.stdout.fileno())) reader.start() @@ -125,16 +128,21 @@ def pipewatch(args): return proc.poll() try: if poll_timeout(proc, 0.5) is None: - proc.terminate() + os.killpg(proc.pid, signal.SIGTERM) if poll_timeout(proc, 0.5) is None: - proc.kill() + os.killpg(proc.pid, signal.SIGKILL) except OSError: pass return poll_timeout(proc, 0.5) # Wait for them to die, or kill them - gret = kill(generator) cret = kill(consumer) + gret = kill(generator) + + # Consume all remaining data in the queue until the reader + # and watcher threads are done + while reader.is_alive() or watcher.is_alive(): + queue.get(True, 0.1) fprintf(sys.stderr, "pipewatch: generator returned %d, " + "consumer returned %d\n", gret, cret)