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.pid import (
check_pid_process,
cleanup_pidfile,
ProcessInTheWay,
InvalidPidFile,
)

View File

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

View File

@ -1,8 +1,5 @@
import os
import psutil
from contextlib import (
contextmanager,
)
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):
"""
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:
f.write("{} {}\n".format(pid, starttime).encode("utf8"))
yield # setup completed, await cleanup
def cleanup_pidfile(pidfile):
"""
Safely clean up a PID-file
"""
try:
pidfile.remove()
except Exception as e:
print(
raise CannotRemovePidFile(
"Couldn't remove '{pidfile}': {err}.".format(
pidfile=pidfile.path,
err=e,