Skip to content
This repository

Start webbrowser in a thread. Prevents lockup with Chrome. #936

Closed
wants to merge 1 commit into from

2 participants

Fernando Perez Min RK
Fernando Perez
Owner

If a user has Chrome set as their default browser (system-wide or via
the BROWSER environment variable), opening the notebook hangs
because the chrome call doesn't return immediately. This solves the
issue by opening the browser in a thread.

Note that there remains an issue where killing the notebook will kill
Chrome if the Chrome session was started by us. I haven't found a way
to work around that despite attempts by making the webbrowser.open()
call in a subprocess.

Fernando Perez Start webbrowser in a thread. Prevents lockup with Chrome.
If a user has Chrome set as their default browser (system-wide or via
the `BROWSER` environment variable), opening the notebook hangs
because the chrome call doesn't return immediately.  This solves the
issue by opening the browser in a thread.

Note that there remains an issue where killing the notebook will kill
Chrome if the Chrome session was started by us.  I haven't found a way
to work around that despite attempts by making the webbrowser.open()
call in a subprocess.
9f23711
Min RK
Owner

Sensible, though note that it is actually only affects setting the BROWSER env to a blocking call. It does not affect Chrome, or any other browser, if it is the default system-wide as you suggest (assuming support for gnome-open or similar).

I actually was able to get the subprocess to survive notebook termination (at least with sigint/ctrl-C), with:

            def b():
                signal.signal(signal.SIGINT, signal.SIG_IGN)
                webbrowser.open("%s://%s:%i" % (proto, ip, self.port),
                                         new=2)
            p = multiprocessing.Process(target=b)
            p.daemon = True
            p.start()
Fernando Perez
Owner

Right on the system-wide setting, which is good.

I tried several approaches with subprocess and threading, but not the one with mp (I basically wrote the identical code you have with threading instead of mp). I'd say we go with that approach instead, as it will ultimately provide a better interface.

How does that sound? I can update the PR in a bit.
I'll

Fernando Perez
Owner

Mmh: I just implemented the code above, and I still get the Chrome session to die on my system (ubuntu 11.10, Chrome stable)... No luck, it dies just like with the threading or subprocess approach. Any chance when you were testing you had a Chrome process lying around beforehand? In that case it doesn't get killed...

Min RK
Owner

I did check, but I may not have been thorough. I honestly don't think it's a big deal - this is only going to affect users who don't actually use Chrome, but are running the notebook with Chrome (e.g. testers, like you). The more important thing is it starts up functional, which is resolved by the threading.

Since such users are few, and presumably fairly advanced, we can just note that using BROWSER='google-chrome&' (background) instead of BROWSER='google-chrome' should avoid the problem, though I'm not 100% sure it will be different from the multiprocessing behavior.

Fernando Perez
Owner
Fernando Perez
Owner

Rebased to avoid recursive merge, pushed.

Fernando Perez fperez closed this October 27, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Oct 27, 2011
Fernando Perez Start webbrowser in a thread. Prevents lockup with Chrome.
If a user has Chrome set as their default browser (system-wide or via
the `BROWSER` environment variable), opening the notebook hangs
because the chrome call doesn't return immediately.  This solves the
issue by opening the browser in a thread.

Note that there remains an issue where killing the notebook will kill
Chrome if the Chrome session was started by us.  I haven't found a way
to work around that despite attempts by making the webbrowser.open()
call in a subprocess.
9f23711
This page is out of date. Refresh to see the latest.
9  IPython/frontend/html/notebook/notebookapp.py
@@ -16,14 +16,17 @@
16 16
 # Imports
17 17
 #-----------------------------------------------------------------------------
18 18
 
  19
+# stdlib
19 20
 import errno
20 21
 import logging
21 22
 import os
22 23
 import signal
23 24
 import socket
24 25
 import sys
  26
+import threading
25 27
 import webbrowser
26 28
 
  29
+# Third party
27 30
 import zmq
28 31
 
29 32
 # Install the pyzmq ioloop. This has to be done before anything else from
@@ -35,6 +38,7 @@
35 38
 from tornado import httpserver
36 39
 from tornado import web
37 40
 
  41
+# Our own libraries
38 42
 from .kernelmanager import MappingKernelManager
39 43
 from .handlers import (LoginHandler,
40 44
     ProjectDashboardHandler, NewHandler, NamedNotebookHandler,
@@ -301,7 +305,10 @@ def start(self):
301 305
                                                                           self.port))
302 306
         if self.open_browser:
303 307
             ip = self.ip or '127.0.0.1'
304  
-            webbrowser.open("%s://%s:%i" % (proto, ip, self.port), new=2)
  308
+            b = lambda : webbrowser.open("%s://%s:%i" % (proto, ip, self.port),
  309
+                                         new=2)
  310
+            threading.Thread(target=b).start()
  311
+            
305 312
         ioloop.IOLoop.instance().start()
306 313
 
307 314
 #-----------------------------------------------------------------------------
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.