Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
def it_passes_when_there_are_no_locks(self, tmpdir):
assert show_runaway_processes(tmpdir.strpath) is None
def it_fails_when_there_are_locks(self, tmpdir):
lockfile = tmpdir.ensure('lock')
lock = lockfile.open()
with ShouldRaise(LockHeld):
show_runaway_processes(lockfile.strpath)
lock.close()
show_runaway_processes(lockfile.strpath)
def handle_race(path):
show_runaway_processes(path)
if handle_race.limit > 0:
handle_race.limit -= 1
else:
reraise(Impossible('lock is held, but not by any process, ten times'))
handle_race.limit = 10