annotate mercurial/testing/ps_util.py @ 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
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
51996
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
1 # This python code can be imported into tests in order to terminate a process
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
2 # with signal.SIGKILL on posix, or a roughly equivalent procedure on Windows.
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
3 import os
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
4 import signal
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
5 import subprocess
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
6 import sys
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
7 import tempfile
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
8
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
9 from .. import (
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
10 encoding,
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
11 pycompat,
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
12 )
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
13
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
14 from ..utils import procutil
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
15
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
16
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
17 def kill_nt(pid: int, exit_code: int):
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
18 fd, pidfile = tempfile.mkstemp(
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
19 prefix=b"sigkill-", dir=encoding.environ[b"HGTMP"], text=False
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
20 )
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
21 try:
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
22 os.write(fd, b'%d\n' % pid)
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
23 finally:
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
24 os.close(fd)
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
25
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
26 env = dict(encoding.environ)
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
27 env[b"DAEMON_EXITCODE"] = b"%d" % exit_code
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
28
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
29 # Simulate the message written to stderr for this process on non-Windows
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
30 # platforms, for test consistency.
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
31 print("Killed!", file=sys.stderr)
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
32
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
33 subprocess.run(
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
34 [
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
35 encoding.environ[b"PYTHON"],
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
36 b"%s/killdaemons.py"
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
37 % encoding.environ[b'RUNTESTDIR_FORWARD_SLASH'],
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
38 pidfile,
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
39 ],
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
40 env=procutil.tonativeenv(env),
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
41 )
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
42
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
43
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
44 def kill(pid: int):
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
45 """Kill the process with the given PID with SIGKILL or equivalent."""
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
46 if pycompat.iswindows:
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
47 exit_code = 128 + 9
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
48 kill_nt(pid, exit_code)
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
49 else:
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
50 os.kill(pid, signal.SIGKILL)