4848import atexit
4949import signal
5050import sys
51+ import shutil
5152
5253
5354class JupyterServerInstance :
@@ -112,7 +113,7 @@ def find_free_port():
112113 :return: A free port number (int).
113114 """
114115 with socket .socket () as sock :
115- sock .bind (("" , 0 ))
116+ sock .bind (("127.0.0.1 " , 0 ))
116117 return sock .getsockname ()[1 ]
117118
118119 def is_server_running (self , retries = 10 , delay = 0.2 ):
@@ -183,6 +184,9 @@ def stop_server(self):
183184
184185 # Check if the process with the given PID is a Jupyter server
185186 try :
187+ ps_cmd = shutil .which ("ps" )
188+ if not ps_cmd :
189+ raise RuntimeError (_ ("Unable to find 'ps' command in PATH." ))
186190 proc_name = (
187191 subprocess .check_output (["ps" , "-p" , str (self .pid ), "-o" , "args=" ])
188192 .decode ()
@@ -194,12 +198,17 @@ def stop_server(self):
194198 "Process with PID {} is not a Jupyter server: found '{}'."
195199 ).format (self .pid , proc_name )
196200 )
197- except subprocess .CalledProcessError :
198- raise RuntimeError (_ ("No process found with PID {}." ).format (self .pid ))
201+ except subprocess .CalledProcessError as e :
202+ raise RuntimeError (
203+ _ ("No process found with PID {}." ).format (self .pid )
204+ ) from e
199205
200206 # Attempt to terminate the server process
201207 if self .is_server_running (self .port ):
202208 try :
209+ kill_cmd = shutil .which ("kill" )
210+ if not kill_cmd :
211+ raise RuntimeError (_ ("Unable to find 'kill' command in PATH." ))
203212 subprocess .check_call (["kill" , str (self .pid )])
204213 except subprocess .CalledProcessError as e :
205214 raise RuntimeError (
0 commit comments