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)

cherrypy/process/plugins.py

                 pass
     
     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))
             
             try:
-                _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__)
         else:
             if self.thread is not threading.currentThread():
+                name = self.thread.getName()
                 self.thread.cancel()
                 self.thread.join()
-                self.bus.log("Stopped thread %r." % self.thread.getName())
+                self.bus.log("Stopped thread %r." % name)
             self.thread = None
     
     def graceful(self):

cherrypy/test/test_states.py

 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 = """
     else:
         ssl = ""
     
-    if starterror:
-        starterror = "starterror: True"
-    else:
-        starterror = ""
-    
     conffile = open(os.path.join(thisdir, 'test_states.conf'), 'wb')
     conffile.write("""[global]
 server.socket_host: '%(host)s'
 log.error_file: r'%(error_log)s'
 log.access_file: r'%(access_log)s'
 %(ssl)s
-%(starterror)s
+%(extra)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,
        })
     conffile.close()
 
         
         # 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:
             self.fail("Process failed to return nonzero exit code.")
             return
         
         # 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)
         wait(pid)
         # 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:
+            self.fail("Old SIGTERM handler did not run.\n%r" % target_line)
 
 
 cases = [v for v in globals().values()

cherrypy/test/test_states_demo.py

         cherrypy.engine.exit()
     exit.exposed = True
     
-    def unsub_sig(self):
-        cherrypy.engine.signal_handler.unsubscribe()
-        return "OK"
-    unsub_sig.exposed = True
-
 try:
-    from signal import SIGTERM
+    from signal import signal, SIGTERM
 except ImportError:
     pass
 else:
     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 ProjectModifiedEvent.java.
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.