Mercurial > hg
changeset 51996:625cf9621551
tests: add a module that can perform the equivalent of `SIGKILL` on any OS
I started with this being Windows specific, but let's push all of the decision
making into this function so that it can just be called by the tests. The
tradeoff is that this is very specific to sending `SIGKILL`- since
`signal.SIGKILL` doesn't exist on Windows, the desired signal can't be passed
from the caller. Maybe there's a way, but let's wait until there's a need.
We don't use `killdaemons.py` unconditionally because it starts with a more
graceful `SIGTERM` on posix.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Sat, 12 Oct 2024 16:06:37 -0400 |
parents | 23cc79e04c28 |
children | 3f70ea5bcaeb |
files | mercurial/testing/ps_util.py |
diffstat | 1 files changed, 50 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mercurial/testing/ps_util.py Sat Oct 12 16:06:37 2024 -0400 @@ -0,0 +1,50 @@ +# This python code can be imported into tests in order to terminate a process +# with signal.SIGKILL on posix, or a roughly equivalent procedure on Windows. +import os +import signal +import subprocess +import sys +import tempfile + +from .. import ( + encoding, + pycompat, +) + +from ..utils import procutil + + +def kill_nt(pid: int, exit_code: int): + fd, pidfile = tempfile.mkstemp( + prefix=b"sigkill-", dir=encoding.environ[b"HGTMP"], text=False + ) + try: + os.write(fd, b'%d\n' % pid) + finally: + os.close(fd) + + env = dict(encoding.environ) + env[b"DAEMON_EXITCODE"] = b"%d" % exit_code + + # Simulate the message written to stderr for this process on non-Windows + # platforms, for test consistency. + print("Killed!", file=sys.stderr) + + subprocess.run( + [ + encoding.environ[b"PYTHON"], + b"%s/killdaemons.py" + % encoding.environ[b'RUNTESTDIR_FORWARD_SLASH'], + pidfile, + ], + env=procutil.tonativeenv(env), + ) + + +def kill(pid: int): + """Kill the process with the given PID with SIGKILL or equivalent.""" + if pycompat.iswindows: + exit_code = 128 + 9 + kill_nt(pid, exit_code) + else: + os.kill(pid, signal.SIGKILL)