use stdin-closing for pidfile cleanup too

This commit is contained in:
meejah 2022-09-15 21:51:56 -06:00
parent 6048d1d9a9
commit 642b604753
3 changed files with 15 additions and 15 deletions

View File

@ -30,6 +30,7 @@ from allmydata.util.configutil import UnknownConfigError
from allmydata.util.deferredutil import HookMixin from allmydata.util.deferredutil import HookMixin
from allmydata.util.pid import ( from allmydata.util.pid import (
check_pid_process, check_pid_process,
cleanup_pidfile,
ProcessInTheWay, ProcessInTheWay,
InvalidPidFile, InvalidPidFile,
) )

View File

@ -176,14 +176,8 @@ class RunTests(SyncTestCase):
config['basedir'] = basedir.path config['basedir'] = basedir.path
config.twistd_args = [] config.twistd_args = []
class DummyRunner:
runs = [] runs = []
_exitSignal = None result_code = run(config, runApp=runs.append)
def run(self):
self.runs.append(True)
result_code = run(config, runner=DummyRunner())
self.assertThat( self.assertThat(
config.stderr.getvalue(), config.stderr.getvalue(),
Contains("found invalid PID file in"), Contains("found invalid PID file in"),
@ -191,7 +185,7 @@ class RunTests(SyncTestCase):
# because the pidfile is invalid we shouldn't get to the # because the pidfile is invalid we shouldn't get to the
# .run() call itself. # .run() call itself.
self.assertThat( self.assertThat(
DummyRunner.runs, runs,
Equals([]) Equals([])
) )

View File

@ -1,8 +1,5 @@
import os import os
import psutil import psutil
from contextlib import (
contextmanager,
)
class ProcessInTheWay(Exception): class ProcessInTheWay(Exception):
@ -17,7 +14,12 @@ class InvalidPidFile(Exception):
""" """
@contextmanager class CannotRemovePidFile(Exception):
"""
something went wrong removing the pidfile
"""
def check_pid_process(pidfile, find_process=None): def check_pid_process(pidfile, find_process=None):
""" """
If another instance appears to be running already, raise an If another instance appears to be running already, raise an
@ -72,12 +74,15 @@ def check_pid_process(pidfile, find_process=None):
with pidfile.open("w") as f: with pidfile.open("w") as f:
f.write("{} {}\n".format(pid, starttime).encode("utf8")) f.write("{} {}\n".format(pid, starttime).encode("utf8"))
yield # setup completed, await cleanup
def cleanup_pidfile(pidfile):
"""
Safely clean up a PID-file
"""
try: try:
pidfile.remove() pidfile.remove()
except Exception as e: except Exception as e:
print( raise CannotRemovePidFile(
"Couldn't remove '{pidfile}': {err}.".format( "Couldn't remove '{pidfile}': {err}.".format(
pidfile=pidfile.path, pidfile=pidfile.path,
err=e, err=e,