Tweak PollScheduler signal handling.
authorZac Medico <zmedico@gentoo.org>
Sun, 16 Jan 2011 03:43:35 +0000 (19:43 -0800)
committerZac Medico <zmedico@gentoo.org>
Sun, 16 Jan 2011 20:33:58 +0000 (12:33 -0800)
bin/egencache
pym/_emerge/Scheduler.py
pym/_emerge/actions.py

index bf729c380bd50958b97355d9370ccc4883a05a94..76dd900e7fb7ca36b48d29f8ab03264f3fc1e881 100755 (executable)
@@ -272,15 +272,19 @@ class GenCache(object):
 
                try:
                        self._regen.run()
-
-                       if received_signal:
-                               sys.exit(received_signal[0])
                finally:
                        # Restore previous handlers
                        if earlier_sigint_handler is not None:
                                signal.signal(signal.SIGINT, earlier_sigint_handler)
+                       else:
+                               signal.signal(signal.SIGINT, signal.SIG_DFL)
                        if earlier_sigterm_handler is not None:
                                signal.signal(signal.SIGTERM, earlier_sigterm_handler)
+                       else:
+                               signal.signal(signal.SIGTERM, signal.SIG_DFL)
+
+               if received_signal:
+                       sys.exit(received_signal[0])
 
                self.returncode |= self._regen.returncode
                cp_missing = self._cp_missing
index c8301a2985c173b6daaba6da5a598b4cdd31176f..7f38dedc4e198ac551de30c85257d2d0204e99ea 100644 (file)
@@ -7,6 +7,7 @@ import gc
 import gzip
 import logging
 import shutil
+import signal
 import sys
 import tempfile
 import textwrap
@@ -1164,10 +1165,35 @@ class Scheduler(PollScheduler):
                        return rval
 
                while True:
-                       rval = self._merge()
 
-                       if self._terminated.is_set():
-                               return 1
+                       received_signal = []
+
+                       def sighandler(signum, frame):
+                               signal.signal(signal.SIGINT, signal.SIG_IGN)
+                               signal.signal(signal.SIGTERM, signal.SIG_IGN)
+                               portage.util.writemsg("\n\nExiting on signal %(signal)s\n" % \
+                                       {"signal":signum})
+                               self.terminate()
+                               received_signal.append(128 + signum)
+
+                       earlier_sigint_handler = signal.signal(signal.SIGINT, sighandler)
+                       earlier_sigterm_handler = signal.signal(signal.SIGTERM, sighandler)
+
+                       try:
+                               rval = self._merge()
+                       finally:
+                               # Restore previous handlers
+                               if earlier_sigint_handler is not None:
+                                       signal.signal(signal.SIGINT, earlier_sigint_handler)
+                               else:
+                                       signal.signal(signal.SIGINT, signal.SIG_DFL)
+                               if earlier_sigterm_handler is not None:
+                                       signal.signal(signal.SIGTERM, earlier_sigterm_handler)
+                               else:
+                                       signal.signal(signal.SIGTERM, signal.SIG_DFL)
+
+                       if received_signal:
+                               sys.exit(received_signal[0])
 
                        if rval == os.EX_OK or fetchonly or not keep_going:
                                break
index ab2653e7649ab1862b0f71a95e7850eab00b68b4..1ea2510d25487ff6630ff3cf66e6c7ce56adcccc 100644 (file)
@@ -147,7 +147,6 @@ def action_build(settings, trees, mtimedb,
 
        ldpath_mtimes = mtimedb["ldpath"]
        favorites=[]
-       merge_count = 0
        buildpkgonly = "--buildpkgonly" in myopts
        pretend = "--pretend" in myopts
        fetchonly = "--fetchonly" in myopts or "--fetch-all-uri" in myopts
@@ -435,32 +434,7 @@ def action_build(settings, trees, mtimedb,
                del mydepgraph
                clear_caches(trees)
 
-               received_signal = []
-
-               def emergeexitsig(signum, frame):
-                       signal.signal(signal.SIGINT, signal.SIG_IGN)
-                       signal.signal(signal.SIGTERM, signal.SIG_IGN)
-                       portage.util.writemsg("\n\nExiting on signal %(signal)s\n" % \
-                               {"signal":signum})
-                       mergetask.terminate()
-                       received_signal.append(128 + signum)
-
-               earlier_sigint_handler = signal.signal(signal.SIGINT, emergeexitsig)
-               earlier_sigterm_handler = signal.signal(signal.SIGTERM, emergeexitsig)
-
-               try:
-                       retval = mergetask.merge()
-
-                       if received_signal:
-                               sys.exit(received_signal[0])
-               finally:
-                       # Restore previous handlers
-                       if earlier_sigint_handler is not None:
-                               signal.signal(signal.SIGINT, earlier_sigint_handler)
-                       if earlier_sigterm_handler is not None:
-                               signal.signal(signal.SIGTERM, earlier_sigterm_handler)
-
-               merge_count = mergetask.curval
+               retval = mergetask.merge()
 
                if retval == os.EX_OK and not (buildpkgonly or fetchonly or pretend):
                        if "yes" == settings.get("AUTOCLEAN"):
@@ -1843,15 +1817,19 @@ def action_regen(settings, portdb, max_jobs, max_load):
 
        try:
                regen.run()
-
-               if received_signal:
-                       sys.exit(received_signal[0])
        finally:
                # Restore previous handlers
                if earlier_sigint_handler is not None:
                        signal.signal(signal.SIGINT, earlier_sigint_handler)
+               else:
+                       signal.signal(signal.SIGINT, signal.SIG_DFL)
                if earlier_sigterm_handler is not None:
                        signal.signal(signal.SIGTERM, earlier_sigterm_handler)
+               else:
+                       signal.signal(signal.SIGTERM, signal.SIG_DFL)
+
+       if received_signal:
+               sys.exit(received_signal[0])
 
        portage.writemsg_stdout("done!\n")
        return regen.returncode