##// END OF EJS Templates
Check for pids when listing nbserver processes
Thomas Kluyver -
Show More
@@ -74,6 +74,7 b' from IPython.kernel.zmq.session import default_secure, Session'
74 from IPython.nbformat.sign import NotebookNotary
74 from IPython.nbformat.sign import NotebookNotary
75 from IPython.utils.importstring import import_item
75 from IPython.utils.importstring import import_item
76 from IPython.utils import submodule
76 from IPython.utils import submodule
77 from IPython.utils.process import check_pid
77 from IPython.utils.traitlets import (
78 from IPython.utils.traitlets import (
78 Dict, Unicode, Integer, List, Bool, Bytes, Instance,
79 Dict, Unicode, Integer, List, Bool, Bytes, Instance,
79 DottedObjectName, TraitError,
80 DottedObjectName, TraitError,
@@ -844,6 +845,7 b' class NotebookApp(BaseIPythonApplication):'
844 'secure': bool(self.certfile),
845 'secure': bool(self.certfile),
845 'base_url': self.base_url,
846 'base_url': self.base_url,
846 'notebook_dir': os.path.abspath(self.notebook_dir),
847 'notebook_dir': os.path.abspath(self.notebook_dir),
848 'pid': os.getpid()
847 }
849 }
848
850
849 def write_server_info_file(self):
851 def write_server_info_file(self):
@@ -917,8 +919,17 b" def list_running_servers(profile='default'):"
917 for file in os.listdir(pd.security_dir):
919 for file in os.listdir(pd.security_dir):
918 if file.startswith('nbserver-'):
920 if file.startswith('nbserver-'):
919 with io.open(os.path.join(pd.security_dir, file), encoding='utf-8') as f:
921 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)
921
923
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
922 #-----------------------------------------------------------------------------
933 #-----------------------------------------------------------------------------
923 # Main entry point
934 # Main entry point
924 #-----------------------------------------------------------------------------
935 #-----------------------------------------------------------------------------
@@ -16,6 +16,8 b' This file is only meant to be imported by process.py, not by end-users.'
16 from __future__ import print_function
16 from __future__ import print_function
17
17
18 # Stdlib
18 # Stdlib
19 import errno
20 import os
19 import subprocess as sp
21 import subprocess as sp
20 import sys
22 import sys
21
23
@@ -209,5 +211,15 b' class ProcessHandler(object):'
209 # (ls is a good example) that makes them hard.
211 # (ls is a good example) that makes them hard.
210 system = ProcessHandler().system
212 system = ProcessHandler().system
211
213
212
214 def check_pid(pid):
213
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
@@ -185,3 +185,8 b' try:'
185 return result
185 return result
186 except AttributeError:
186 except AttributeError:
187 arg_split = py_arg_split
187 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))
@@ -21,11 +21,11 b' import sys'
21
21
22 # Our own
22 # Our own
23 if sys.platform == 'win32':
23 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
25 elif sys.platform == 'cli':
25 elif sys.platform == 'cli':
26 from ._process_cli import _find_cmd, system, getoutput, arg_split
26 from ._process_cli import _find_cmd, system, getoutput, arg_split
27 else:
27 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
29
29
30 from ._process_common import getoutputerror, get_output_error_code, process_handler
30 from ._process_common import getoutputerror, get_output_error_code, process_handler
31 from . import py3compat
31 from . import py3compat
General Comments 0
You need to be logged in to leave comments. Login now