28
28
class ServerNotDead (Exception ):
29
29
pass
30
30
31
+ class CannotFindServer (Exception ):
32
+ pass
33
+
31
34
32
35
def get_ephemeral_host (cached = True , regen_cache = False ):
33
36
"""
@@ -305,6 +308,7 @@ def start_server(self, env=None):
305
308
306
309
def _signal (self , pid , signal ):
307
310
try :
311
+ log .debug (f'Signalling pid { pid } with signal { signal } ' )
308
312
os .kill (pid , signal )
309
313
except OSError as oe :
310
314
if oe .errno == errno .ESRCH : # Process doesn't appear to exist.
@@ -332,24 +336,27 @@ def kill_by_pid(self, pid, retries=5):
332
336
if not self ._kill_with (signal .SIGKILL , retries ):
333
337
raise ServerNotDead (f"Server not dead after { retries } retries" )
334
338
339
+ def _find_pids_by_port (self ):
340
+ if OSX :
341
+ netstat_cmd = "lsof -n -i:{} | grep LISTEN | awk '{{ print $2 }}'" .format (self .port )
342
+ else :
343
+ netstat_cmd = ("netstat -anp 2>/dev/null | grep %s:%s | grep LISTEN | "
344
+ "awk '{ print $7 }' | cut -d'/' -f1" % (socket .gethostbyname (self .hostname ), self .port ))
345
+ pids = [p .strip () for p in self .run (netstat_cmd , capture = True , cd = '/' ).split ('\n ' ) if p .strip ()]
346
+ if pids :
347
+ log .debug (f"Found pids: { pids } " )
348
+ else :
349
+ log .debug (f"No pids found" )
350
+ return pids
351
+
335
352
def _find_and_kill_by_port (self , retries , signal ):
336
353
log .debug ("Killing server running at {}:{} using signal {}" .format (self .hostname , self .port , signal ))
354
+ pids = self ._find_pids_by_port ()
355
+ if not pids :
356
+ raise CannotFindServer ()
337
357
for _ in range (retries ):
338
- if OSX :
339
- netstat_cmd = "lsof -n -i:{} | grep LISTEN | awk '{{ print $2 }}'" .format (self .port )
340
- else :
341
- netstat_cmd = ("netstat -anp 2>/dev/null | grep %s:%s | grep LISTEN | "
342
- "awk '{ print $7 }' | cut -d'/' -f1" % (socket .gethostbyname (self .hostname ), self .port ))
343
- pids = [p .strip () for p in self .run (netstat_cmd , capture = True , cd = '/' ).split ('\n ' ) if p .strip ()]
344
- if pids :
345
- log .debug (f"Found pids: { pids } " )
346
- else :
347
- log .debug (f"No pids found" )
348
-
349
358
if not pids :
350
- # No PIDs remaining, server has died.
351
- break
352
-
359
+ return
353
360
for pid in pids :
354
361
try :
355
362
pid = int (pid )
@@ -358,6 +365,7 @@ def _find_and_kill_by_port(self, retries, signal):
358
365
else :
359
366
self ._signal (pid , signal )
360
367
time .sleep (self .kill_retry_delay )
368
+ pids = self ._find_pids_by_port ()
361
369
else :
362
370
raise ServerNotDead ("Server not dead after %d retries" % retries )
363
371
@@ -376,12 +384,15 @@ def kill(self, retries=5):
376
384
377
385
try :
378
386
self ._find_and_kill_by_port (retries , self .kill_signal )
387
+ except CannotFindServer :
388
+ log .debug (f"Server can't be found listening on port { self .port } " )
389
+ return
379
390
except ServerNotDead :
380
391
log .error ("Server not dead after %d retries, trying with SIGKILL" % retries )
381
- try :
382
- self ._find_and_kill_by_port (retries , signal .SIGKILL )
383
- except ServerNotDead :
384
- log .error ("Server still not dead, giving up" )
392
+ try :
393
+ self ._find_and_kill_by_port (retries , signal .SIGKILL )
394
+ except ServerNotDead :
395
+ log .error ("Server still not dead, giving up" )
385
396
386
397
def teardown (self ):
387
398
""" Called when tearing down this instance, eg in a context manager
0 commit comments