def it_kills_processes_holding_the_lock(self, tmpdir): lockfile = tmpdir.ensure('lock') lock = lockfile.open() process = Popen(('sleep', 'infinity')) lock.close() # assert `process` has `lock` assert list(fuser(lockfile.strpath)) == [process.pid] with mock.patch('sys.stderr', new_callable=StringIO.StringIO) as mock_stderr: terminate_runaway_processes(lockfile.strpath) assert 'WARNING: Killing these runaway ' in mock_stderr.getvalue() wait_for(lambda: process.poll() == -9)
def wait4(tmpdir): """wait for all subprocesses to finish.""" lock = tmpdir.join('pytest-subprocesses.lock') try: from pgctl.flock import flock with flock(lock.strpath): yield finally: from pgctl.fuser import fuser from pgctl.functions import ps fusers = True i = 0 while fusers and i < 10: fusers = tuple(fuser(lock.strpath)) i += 1 # we only hit this when tests are broken. pragma: no cover fusers = ps(fusers) if fusers: raise AssertionError("there's a subprocess that's still running:\n%s" % ps(fusers))
def wait4(tmpdir): """wait for all subprocesses to finish.""" lock = tmpdir.join('pytest-subprocesses.lock') try: from pgctl.flock import flock with flock(lock.strpath): yield finally: from pgctl.fuser import fuser from pgctl.functions import ps fusers = True i = 0 while fusers and i < 10: fusers = tuple(fuser(lock.strpath)) i += 1 # we only hit this when tests are broken. pragma: no cover fusers = ps(fusers) if fusers: raise AssertionError( "there's a subprocess that's still running:\n%s" % ps(fusers))
def clean_service(service_path): # we use SIGTERM; SIGKILL is cheating. limit = 100 while limit > 0: # pragma: no branch: we don't expect to ever hit the limit assert os.path.isdir(service_path), service_path try: show_runaway_processes(service_path) print('lock released -- done.') break except LockHeld: print('lock held -- killing!') for pid in fuser(service_path): try: os.system('ps -fj %i' % pid) os.kill(pid, signal.SIGTERM) except OSError as error: # race condition -- process stopped between list and kill :pragma: no-cover if error.errno == 3: # no such process pass else: raise limit -= 1