bugfix: shutdown_server returns True when pid exists #4674
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The Bug
check_pid
returnsTrue
if the PID for a notebook server still exists. Therefore, theif check_pid(pid):
statements here and here evaluate toTrue
even though the notebook server is still running. This is not the expected behaviour, since this means that theshutdown_server
function will return and returnTrue
even though the notebook server is still in the process of shutting down.Reproducing the Bug
The following procedure was tested in a python3.7 virtual environment with jupyter 4.4.0
jupyter notebook --port=8888
while True: pass
python -c 'from notebook.notebookapp import shutdown_server, list_running_servers; port=8888; servers = [x for x in list_running_servers() if x["port"] == port]; status = True if len(servers) == 0 else shutdown_server(servers[0]); print(status)' && bash -c 'ps -ef | grep "jupyter-notebook"'
a)
shutdown_server
returnsTrue
and then,b) the
jupyter-notebook
process is still running`In step 4, the expected behaviour would be for the output of the
bash -c 'ps -ef | grep "jupyter-notebook"'
command to not show a jupyter notebook server running.Note: the while loop helps make this bug more reproducible. It's intermittent without the loop due to factors such as system load and machine specs. Also, that big command in step 3 is importing
shutdown_server
and then running it to kill the notebook server we started in step 1, that's all.For step 3, here's the output I get on my system:
Notice that the
jupyter-notebook --port=8888
process is still running, despiteshutdown_server
telling us that the server was shutdown. Also notice that step 3 executes very fast, yet the jupyter notebook app will take several seconds to shutdown.Note: the notebook server does eventually shutdown, [due to the api call on these] lines(https://github.com/jupyter/notebook/blob/master/notebook/notebookapp.py#L413-L420) succeeding. The bug here is that the shutdown happens after the
shutdown_server
function returns, instead of happening beforeshudown_server
returns.Who Cares?
I'm working on a project where we are running jupyter notebooks in docker containers. We need to be able to shutdown the notebooks by sending a shutdown command of some kind to the notebook server running in the docker container. Using the
shutdown_server
function didn't result in the expected behaviour, so I started digging in to why and found this bug.The Fix
This commit adds a
not
to each line:if not check_pid(pid):
so that the conditional only evaluates toTrue
ifcheck_pid
returnsFalse
, which happens when the notebook server has shutdown, as expected.After applying the fix, the output of steps 1 through 3 detailed above is as expected:
you will also notice that it step 3 will take as much time to execute as it takes for the jupyter notebook server to shutdown.