48
48
import atexit
49
49
import signal
50
50
import sys
51
+ import shutil
51
52
52
53
53
54
class JupyterServerInstance :
@@ -112,7 +113,7 @@ def find_free_port():
112
113
:return: A free port number (int).
113
114
"""
114
115
with socket .socket () as sock :
115
- sock .bind (("" , 0 ))
116
+ sock .bind (("127.0.0.1 " , 0 ))
116
117
return sock .getsockname ()[1 ]
117
118
118
119
def is_server_running (self , retries = 10 , delay = 0.2 ):
@@ -183,6 +184,9 @@ def stop_server(self):
183
184
184
185
# Check if the process with the given PID is a Jupyter server
185
186
try :
187
+ ps_cmd = shutil .which ("ps" )
188
+ if not ps_cmd :
189
+ raise RuntimeError (_ ("Unable to find 'ps' command in PATH." ))
186
190
proc_name = (
187
191
subprocess .check_output (["ps" , "-p" , str (self .pid ), "-o" , "args=" ])
188
192
.decode ()
@@ -194,12 +198,17 @@ def stop_server(self):
194
198
"Process with PID {} is not a Jupyter server: found '{}'."
195
199
).format (self .pid , proc_name )
196
200
)
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
199
205
200
206
# Attempt to terminate the server process
201
207
if self .is_server_running (self .port ):
202
208
try :
209
+ kill_cmd = shutil .which ("kill" )
210
+ if not kill_cmd :
211
+ raise RuntimeError (_ ("Unable to find 'kill' command in PATH." ))
203
212
subprocess .check_call (["kill" , str (self .pid )])
204
213
except subprocess .CalledProcessError as e :
205
214
raise RuntimeError (
0 commit comments