Permalink
Browse files

cleanup connection files on notebook shutdown

Kernels would not linger, but the KernelManagers are not garbage-collected on shutdown.
This means that connection files for kernels still running at notebook shutdown would not be removed.

Also disable the unnecessary (and actively unhelpful) SIGINT handler inherited from the original
copy/paste from the qt app.
  • Loading branch information...
1 parent e30038a commit 1e91756567b46603db985e09cdaf4fc2ef1f6c92 @minrk committed Dec 20, 2011
Showing with 29 additions and 12 deletions.
  1. +29 −12 IPython/frontend/html/notebook/notebookapp.py
@@ -303,9 +303,6 @@ def parse_command_line(self, argv=None):
self.kernel_argv.append("--KernelApp.parent_appname='%s'"%self.name)
def init_configurables(self):
- # Don't let Qt or ZMQ swallow KeyboardInterupts.
- signal.signal(signal.SIGINT, signal.SIG_DFL)
-
# force Session default to be secure
default_secure(self.config)
# Create a KernelManager and start a kernel.
@@ -322,11 +319,9 @@ def init_logging(self):
# self.log is a child of. The logging module dipatches log messages to a log
# and all of its ancenstors until propagate is set to False.
self.log.propagate = False
-
- @catch_config_error
- def initialize(self, argv=None):
- super(NotebookApp, self).initialize(argv)
- self.init_configurables()
+
+ def init_webapp(self):
+ """initialize tornado webapp and httpserver"""
self.web_app = NotebookWebApplication(
self, self.kernel_manager, self.notebook_manager, self.log,
self.webapp_settings
@@ -339,7 +334,7 @@ def initialize(self, argv=None):
ssl_options = None
self.web_app.password = self.password
self.http_server = httpserver.HTTPServer(self.web_app, ssl_options=ssl_options)
- if ssl_options is None and not self.ip:
+ if ssl_options is None and not self.ip and not (self.read_only and not self.password):
self.log.critical('WARNING: the notebook server is listening on all IP addresses '
'but not using any encryption or authentication. This is highly '
'insecure and not recommended.')
@@ -357,6 +352,23 @@ def initialize(self, argv=None):
else:
self.port = port
break
+
+ @catch_config_error
+ def initialize(self, argv=None):
+ super(NotebookApp, self).initialize(argv)
+ self.init_configurables()
+ self.init_webapp()
+
+ def cleanup_kernels(self):
+ """shutdown all kernels
+
+ The kernels will shutdown themselves when this process no longer exists,
+ but explicit shutdown allows the KernelManagers to cleanup the connection files.
+ """
+ self.log.info('Shutting down kernels')
+ km = self.kernel_manager
+ while km.kernel_ids:
+ km.kill_kernel(km.kernel_ids[0])
def start(self):
ip = self.ip if self.ip else '[all ip addresses on your system]'
@@ -371,15 +383,20 @@ def start(self):
b = lambda : webbrowser.open("%s://%s:%i" % (proto, ip, self.port),
new=2)
threading.Thread(target=b).start()
-
- ioloop.IOLoop.instance().start()
+ try:
+ ioloop.IOLoop.instance().start()
+ except KeyboardInterrupt:
+ info("Interrupted...")
+ finally:
+ self.cleanup_kernels()
+
#-----------------------------------------------------------------------------
# Main entry point
#-----------------------------------------------------------------------------
def launch_new_instance():
- app = NotebookApp()
+ app = NotebookApp.instance()
app.initialize()
app.start()

0 comments on commit 1e91756

Please sign in to comment.