]> arthur.barton.de Git - bup.git/commitdiff
main.py: clean up subprocesses dying on signal.
authorAvery Pennarun <apenwarr@gmail.com>
Tue, 2 Mar 2010 21:17:17 +0000 (16:17 -0500)
committerAvery Pennarun <apenwarr@gmail.com>
Tue, 2 Mar 2010 21:17:17 +0000 (16:17 -0500)
CTRL-C didn't abort 'bup random' properly, and possibly others as well.

main.py

diff --git a/main.py b/main.py
index 8cd22937bda207fd5a7afe859a9b7b9a502e999e..d5b4ccc64a6df4fa931273a124067d9d958a8525 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -1,5 +1,5 @@
 #!/usr/bin/env python
-import sys, os, subprocess
+import sys, os, subprocess, signal
 
 argv = sys.argv
 exe = argv[0]
@@ -103,6 +103,15 @@ else:
     outf = 1
     errf = 2
 
+
+class SigException(Exception):
+    pass
+def handler(signum, frame):
+    raise SigException('signal %d received' % signum)
+
+signal.signal(signal.SIGTERM, handler)
+signal.signal(signal.SIGINT, handler)
+
 ret = 95
 try:
     try:
@@ -112,9 +121,12 @@ try:
     except OSError, e:
         log('%s: %s\n' % (subpath(subcmd), e))
         ret = 98
-    except KeyboardInterrupt, e:
+    except SigException, e:
         ret = 94
 finally:
+    if p and p.poll() == None:
+        os.kill(p.pid, signal.SIGTERM)
+        p.wait()
     if n:
         n.stdin.close()
         try: