Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit eee7172

Browse files
committed
Ensure server start error no race condition.
1 parent 6a653c0 commit eee7172

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

jupyter_dash/_stoppable_thread.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ def get_id(self):
1212

1313
def kill(self):
1414
thread_id = self.get_id()
15-
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
16-
ctypes.c_long(thread_id), ctypes.py_object(SystemExit)
17-
)
18-
if res == 0:
19-
raise ValueError(f"Invalid thread id: {thread_id}")
20-
if res > 1:
21-
ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(thread_id), None)
22-
raise SystemExit("Stopping thread failure")
15+
if thread_id:
16+
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
17+
ctypes.c_long(thread_id), ctypes.py_object(SystemExit)
18+
)
19+
if res == 0:
20+
raise ValueError(f"Invalid thread id: {thread_id}")
21+
if res > 1:
22+
ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(thread_id), None)
23+
raise SystemExit("Stopping thread failure")

jupyter_dash/jupyter_app.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ def run():
314314
pass
315315
except Exception as error:
316316
err_q.put(error)
317+
raise error
317318

318319
thread = StoppableThread(target=run)
319320
thread.setDaemon(True)
@@ -326,34 +327,41 @@ def run():
326327
host=host, port=port, token=JupyterDash._token
327328
)
328329

330+
def _get_error():
331+
try:
332+
err = err_q.get_nowait()
333+
if err:
334+
raise err
335+
except queue.Empty:
336+
pass
337+
329338
# Wait for app to respond to _alive endpoint
330339
@retry(
331340
stop_max_attempt_number=15,
332341
wait_exponential_multiplier=10,
333342
wait_exponential_max=1000
334343
)
335344
def wait_for_app():
345+
_get_error()
336346
try:
337-
err = err_q.get_nowait()
338-
if err:
339-
raise err
340-
except queue.Empty:
341-
pass
342-
req = requests.get(alive_url)
343-
res = req.content.decode()
344-
if req.status_code == 500:
345-
raise Exception(res)
346-
347-
if res != "Alive":
348-
url = "http://{host}:{port}".format(
349-
host=host, port=port, token=JupyterDash._token
350-
)
351-
raise OSError(
352-
"Address '{url}' already in use.\n"
353-
" Try passing a different port to run_server.".format(
354-
url=url
347+
req = requests.get(alive_url)
348+
res = req.content.decode()
349+
if req.status_code != 200:
350+
raise Exception(res)
351+
352+
if res != "Alive":
353+
url = "http://{host}:{port}".format(
354+
host=host, port=port, token=JupyterDash._token
355355
)
356-
)
356+
raise OSError(
357+
"Address '{url}' already in use.\n"
358+
" Try passing a different port to run_server.".format(
359+
url=url
360+
)
361+
)
362+
except requests.ConnectionError as err:
363+
_get_error()
364+
raise err
357365

358366
try:
359367
wait_for_app()
@@ -363,7 +371,7 @@ def wait_for_app():
363371
else:
364372
self._display_in_jupyter(dashboard_url, port, mode, width, height)
365373
except Exception as final_error:
366-
msg = final_error.args[0]
374+
msg = str(final_error)
367375
if msg.startswith('<!'):
368376
display(HTML(msg))
369377
else:

0 commit comments

Comments
 (0)