Skip to content

Commit 101f726

Browse files
committed
Prevent tests from hanging
When the NotebookApp raises an exception on initialization, a test can hang forever. This can happen when passing configuration when customizing NotebookTestBase: ``` class TestMissingExtension(NotebookTestBase): @classmethod def get_argv(cls): argv = super(TestMissingExtension, cls).get_argv() argv.extend( [ "--NotebookApp.session_manager_class=doesnt_exist", ] ) return argv def test_this_will_hang_forever(self): pass ``` Since the exception happens before the try/finally the `Event` will never be triggered. By including the construction and initialization of the notebook in the try/finally we can handle situations like this.
1 parent e498de6 commit 101f726

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

notebook/tests/launchnotebook.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -146,31 +146,31 @@ def start_thread():
146146
if 'asyncio' in sys.modules:
147147
import asyncio
148148
asyncio.set_event_loop(asyncio.new_event_loop())
149-
app = cls.notebook = NotebookApp(
150-
port=cls.port,
151-
port_retries=0,
152-
open_browser=False,
153-
config_dir=cls.config_dir,
154-
data_dir=cls.data_dir,
155-
runtime_dir=cls.runtime_dir,
156-
notebook_dir=cls.notebook_dir,
157-
base_url=cls.url_prefix,
158-
config=config,
159-
allow_root=True,
160-
token=cls.token,
161-
)
162-
# don't register signal handler during tests
163-
app.init_signal = lambda : None
164-
# clear log handlers and propagate to root for nose to capture it
165-
# needs to be redone after initialize, which reconfigures logging
166-
app.log.propagate = True
167-
app.log.handlers = []
168-
app.initialize(argv=cls.get_argv())
169-
app.log.propagate = True
170-
app.log.handlers = []
171-
loop = IOLoop.current()
172-
loop.add_callback(started.set)
173149
try:
150+
app = cls.notebook = NotebookApp(
151+
port=cls.port,
152+
port_retries=0,
153+
open_browser=False,
154+
config_dir=cls.config_dir,
155+
data_dir=cls.data_dir,
156+
runtime_dir=cls.runtime_dir,
157+
notebook_dir=cls.notebook_dir,
158+
base_url=cls.url_prefix,
159+
config=config,
160+
allow_root=True,
161+
token=cls.token,
162+
)
163+
# don't register signal handler during tests
164+
app.init_signal = lambda : None
165+
# clear log handlers and propagate to root for nose to capture it
166+
# needs to be redone after initialize, which reconfigures logging
167+
app.log.propagate = True
168+
app.log.handlers = []
169+
app.initialize(argv=cls.get_argv())
170+
app.log.propagate = True
171+
app.log.handlers = []
172+
loop = IOLoop.current()
173+
loop.add_callback(started.set)
174174
app.start()
175175
finally:
176176
# set the event, so failure to start doesn't cause a hang

0 commit comments

Comments
 (0)