Robert Brewer avatar Robert Brewer committed aad76f3

Fixed sig handler unsubscribe test for Unix, plus an odd race condition in autoreload.

Comments (0)

Files changed (3)


     def unsubscribe(self):
-        for sig, handler in self._previous_handlers.iteritems():
-            signame = self.signals[sig]
+        for signum, handler in self._previous_handlers.iteritems():
+            signame = self.signals[signum]
             if handler is None:
                 self.bus.log("Restoring %s handler to SIG_DFL." % signame)
                 self.bus.log("Restoring %s handler %r." % (signame, handler))
-                _signal.signal(sig, handler)
+                _signal.signal(signum, handler)
             except ValueError:
                 self.bus.log("Unable to restore %s handler %r." %
-                             (signame, handler))
+                             (signame, handler), traceback=True)
     def set_handler(self, signal, listener=None):
         """Subscribe a handler for the given signal (number or name).
             self.bus.log("No thread running for %s." % self.__class__.__name__)
             if self.thread is not threading.currentThread():
+                name = self.thread.getName()
-                self.bus.log("Stopped thread %r." % self.thread.getName())
+                self.bus.log("Stopped thread %r." % name)
             self.thread = None
     def graceful(self):


 PID_file_path = os.path.join(thisdir, 'pid_for_test_daemonize')
-def write_conf(scheme='http', starterror=False):
+def write_conf(scheme='http', extra=""):
     if scheme.lower() == 'https':
         serverpem = os.path.join(thisdir, 'test.pem')
         ssl = """
         ssl = ""
-    if starterror:
-        starterror = "starterror: True"
-    else:
-        starterror = ""
     conffile = open(os.path.join(thisdir, 'test_states.conf'), 'wb')
 server.socket_host: '%(host)s'
 log.error_file: r'%(error_log)s'
 log.access_file: r'%(access_log)s'
 """ % {'host': host,
        'port': port,
        'error_log': os.path.join(thisdir, 'test_states_demo.error.log'),
        'access_log': os.path.join(thisdir, 'test_states_demo.access.log'),
        'ssl': ssl,
-       'starterror': starterror,
+       'extra': extra,
         # If a process errors during start, it should stop the engine
         # and exit with a non-zero exit code.
-        write_conf(scheme=self.scheme, starterror=True)
+        write_conf(scheme=self.scheme, extra="starterror: True")
         exit_code = spawn_cp(wait=True)
         if exit_code == 0:
   "Process failed to return nonzero exit code.")
         # Spawn a normal, undaemonized process.
-        write_conf(scheme=self.scheme)
+        write_conf(scheme=self.scheme, extra="unsubsig: True")
         pid = spawn_cp(wait=False, daemonize=False)
-        self.getPage("/unsub_sig")
-        self.assertBody("OK")
         os.kill(pid, SIGTERM)
         # Assert the old handler ran.
         errlog = os.path.join(thisdir, 'test_states_demo.error.log')
-        self.assertEqual(open(errlog, 'rb').readlines()[-1],
-                         "I am an old SIGTERM handler.")
+        target_line = open(errlog, 'rb').readlines()[-10]
+        if not "I am an old SIGTERM handler." in target_line:
+  "Old SIGTERM handler did not run.\n%r" % target_line)
 cases = [v for v in globals().values()


         cherrypy.engine.exit() = True
-    def unsub_sig(self):
-        cherrypy.engine.signal_handler.unsubscribe()
-        return "OK"
- = True
-    from signal import SIGTERM
+    from signal import signal, SIGTERM
 except ImportError:
     def old_term_handler(signum=None, frame=None):
         cherrypy.log("I am an old SIGTERM handler.")
-    _signal.signal(SIGTERM, old_term_handler)
+        sys.exit(0)
+    signal(SIGTERM, old_term_handler)
+def unsub_sig():
+    if cherrypy.config.get('unsubsig', False):
+        cherrypy.engine.signal_handler.unsubscribe()
+cherrypy.engine.subscribe('start', unsub_sig, priority=100)
 def starterror():
         zerodiv = 1 / 0
 cherrypy.engine.subscribe('start', starterror, priority=6)
 cherrypy.tree.mount(Root(), '/', {'/': {}})
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.