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 03:43:35 +0000 (19:43 -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 ac48f853eadb9bd67707b4f87c7b0d82f17a1c58..463d0d85e70f2769b8e59657c59615796b710a9d 100644 (file)
@@ -7,6 +7,7 @@ import gc
 import gzip
 import logging
 import shutil
+import signal
 import sys
 import tempfile
 import textwrap
@@ -1166,10 +1167,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 25b5ab138605384b7bb0cebc4a9ede51039a2106..ee37bb8213fe1dcf66676fe2c1a9eb2893e824db 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"):
@@ -1853,15 +1827,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