Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c850721

Browse files
committed
Check for pids when listing nbserver processes
1 parent 2bf35f9 commit c850721

4 files changed

Lines changed: 33 additions & 5 deletions

File tree

IPython/html/notebookapp.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
from IPython.nbformat.sign import NotebookNotary
7575
from IPython.utils.importstring import import_item
7676
from IPython.utils import submodule
77+
from IPython.utils.process import check_pid
7778
from IPython.utils.traitlets import (
7879
Dict, Unicode, Integer, List, Bool, Bytes, Instance,
7980
DottedObjectName, TraitError,
@@ -844,6 +845,7 @@ def server_info(self):
844845
'secure': bool(self.certfile),
845846
'base_url': self.base_url,
846847
'notebook_dir': os.path.abspath(self.notebook_dir),
848+
'pid': os.getpid()
847849
}
848850

849851
def write_server_info_file(self):
@@ -917,8 +919,17 @@ def list_running_servers(profile='default'):
917919
for file in os.listdir(pd.security_dir):
918920
if file.startswith('nbserver-'):
919921
with io.open(os.path.join(pd.security_dir, file), encoding='utf-8') as f:
920-
yield json.load(f)
922+
info = json.load(f)
921923

924+
# Simple check whether that process is really still running
925+
if check_pid(info['pid']):
926+
yield info
927+
else:
928+
# If the process has died, try to delete its info file
929+
try:
930+
os.unlink(file)
931+
except OSError:
932+
pass # TODO: This should warn or log or something
922933
#-----------------------------------------------------------------------------
923934
# Main entry point
924935
#-----------------------------------------------------------------------------

IPython/utils/_process_posix.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from __future__ import print_function
1717

1818
# Stdlib
19+
import errno
20+
import os
1921
import subprocess as sp
2022
import sys
2123

@@ -209,5 +211,15 @@ def system(self, cmd):
209211
# (ls is a good example) that makes them hard.
210212
system = ProcessHandler().system
211213

212-
213-
214+
def check_pid(pid):
215+
try:
216+
os.kill(pid, 0)
217+
except OSError as err:
218+
if err.errno == errno.ESRCH:
219+
return False
220+
elif err.errno == errno.EPERM:
221+
# Don't have permission to signal the process - probably means it exists
222+
return True
223+
raise
224+
else:
225+
return True

IPython/utils/_process_win32.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,8 @@ def arg_split(commandline, posix=False, strict=True):
185185
return result
186186
except AttributeError:
187187
arg_split = py_arg_split
188+
189+
def check_pid(pid):
190+
# OpenProcess returns 0 if no such process (of ours) exists
191+
# positive int otherwise
192+
return bool(ctypes.windll.kernel32.OpenProcess(1,0,pid))

IPython/utils/process.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121

2222
# Our own
2323
if sys.platform == 'win32':
24-
from ._process_win32 import _find_cmd, system, getoutput, arg_split
24+
from ._process_win32 import _find_cmd, system, getoutput, arg_split, check_pid
2525
elif sys.platform == 'cli':
2626
from ._process_cli import _find_cmd, system, getoutput, arg_split
2727
else:
28-
from ._process_posix import _find_cmd, system, getoutput, arg_split
28+
from ._process_posix import _find_cmd, system, getoutput, arg_split, check_pid
2929

3030
from ._process_common import getoutputerror, get_output_error_code, process_handler
3131
from . import py3compat

0 commit comments

Comments
 (0)