2828class ServerNotDead (Exception ):
2929 pass
3030
31+ class CannotFindServer (Exception ):
32+ pass
33+
3134
3235def get_ephemeral_host (cached = True , regen_cache = False ):
3336 """
@@ -305,6 +308,7 @@ def start_server(self, env=None):
305308
306309 def _signal (self , pid , signal ):
307310 try :
311+ log .debug (f'Signalling pid { pid } with signal { signal } ' )
308312 os .kill (pid , signal )
309313 except OSError as oe :
310314 if oe .errno == errno .ESRCH : # Process doesn't appear to exist.
@@ -332,24 +336,27 @@ def kill_by_pid(self, pid, retries=5):
332336 if not self ._kill_with (signal .SIGKILL , retries ):
333337 raise ServerNotDead (f"Server not dead after { retries } retries" )
334338
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+
335352 def _find_and_kill_by_port (self , retries , signal ):
336353 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 ()
337357 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-
349358 if not pids :
350- # No PIDs remaining, server has died.
351- break
352-
359+ return
353360 for pid in pids :
354361 try :
355362 pid = int (pid )
@@ -358,6 +365,7 @@ def _find_and_kill_by_port(self, retries, signal):
358365 else :
359366 self ._signal (pid , signal )
360367 time .sleep (self .kill_retry_delay )
368+ pids = self ._find_pids_by_port ()
361369 else :
362370 raise ServerNotDead ("Server not dead after %d retries" % retries )
363371
@@ -376,12 +384,15 @@ def kill(self, retries=5):
376384
377385 try :
378386 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
379390 except ServerNotDead :
380391 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" )
385396
386397 def teardown (self ):
387398 """ Called when tearing down this instance, eg in a context manager
0 commit comments