mirror of
https://chromium.googlesource.com/chromium/tools/depot_tools.git
synced 2026-01-11 18:51:29 +00:00
subprocess2, gerrit_test_case: Use SIGTERM instead of SIGKILL for Windows support
SIGKILL doesn't exist on Windows, but SIGTERM does. For our purposes they do the same thing. Bug: 962263 Change-Id: I16929cf7d1377cd81f71401a48b5a84fe54da493 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1608561 Reviewed-by: Marc-Antoine Ruel <maruel@chromium.org> Commit-Queue: Raul Tambre <raul@tambre.ee> Auto-Submit: Raul Tambre <raul@tambre.ee>
This commit is contained in:
@@ -44,10 +44,6 @@ VOID = object()
|
||||
# Error code when a process was killed because it timed out.
|
||||
TIMED_OUT = -2001
|
||||
|
||||
# Globals.
|
||||
# Set to True if you somehow need to disable this hack.
|
||||
SUBPROCESS_CLEANUP_HACKED = False
|
||||
|
||||
|
||||
class CalledProcessError(subprocess.CalledProcessError):
|
||||
"""Augment the standard exception with more data."""
|
||||
@@ -78,51 +74,11 @@ def kill_pid(pid):
|
||||
# Unable to import 'module'
|
||||
# pylint: disable=no-member,F0401
|
||||
import signal
|
||||
return os.kill(pid, signal.SIGKILL)
|
||||
return os.kill(pid, signal.SIGTERM)
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
def kill_win(process):
|
||||
"""Kills a process with its windows handle.
|
||||
|
||||
Has no effect on other platforms.
|
||||
"""
|
||||
try:
|
||||
# Unable to import 'module'
|
||||
# pylint: disable=import-error
|
||||
import win32process
|
||||
# Access to a protected member _handle of a client class
|
||||
# pylint: disable=protected-access
|
||||
return win32process.TerminateProcess(process._handle, -1)
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
def add_kill():
|
||||
"""Adds kill() method to subprocess.Popen for python <2.6"""
|
||||
if hasattr(subprocess.Popen, 'kill'):
|
||||
return
|
||||
|
||||
if sys.platform == 'win32':
|
||||
subprocess.Popen.kill = kill_win
|
||||
else:
|
||||
subprocess.Popen.kill = lambda process: kill_pid(process.pid)
|
||||
|
||||
|
||||
def hack_subprocess():
|
||||
"""subprocess functions may throw exceptions when used in multiple threads.
|
||||
|
||||
See http://bugs.python.org/issue1731717 for more information.
|
||||
"""
|
||||
global SUBPROCESS_CLEANUP_HACKED
|
||||
if not SUBPROCESS_CLEANUP_HACKED and threading.activeCount() != 1:
|
||||
# Only hack if there is ever multiple threads.
|
||||
# There is no point to leak with only one thread.
|
||||
subprocess._cleanup = lambda: None
|
||||
SUBPROCESS_CLEANUP_HACKED = True
|
||||
|
||||
|
||||
def get_english_env(env):
|
||||
"""Forces LANG and/or LANGUAGE to be English.
|
||||
|
||||
@@ -208,10 +164,6 @@ class Popen(subprocess.Popen):
|
||||
popen_lock = threading.Lock()
|
||||
|
||||
def __init__(self, args, **kwargs):
|
||||
# Make sure we hack subprocess if necessary.
|
||||
hack_subprocess()
|
||||
add_kill()
|
||||
|
||||
env = get_english_env(kwargs.get('env'))
|
||||
if env:
|
||||
kwargs['env'] = env
|
||||
|
||||
@@ -352,9 +352,9 @@ class GerritTestCase(unittest.TestCase):
|
||||
return
|
||||
|
||||
# If we get here, the gerrit process is still alive. Send the process
|
||||
# SIGKILL for good measure.
|
||||
# SIGTERM for good measure.
|
||||
try:
|
||||
os.kill(gerrit_instance.gerrit_pid, signal.SIGKILL)
|
||||
os.kill(gerrit_instance.gerrit_pid, signal.SIGTERM)
|
||||
except OSError:
|
||||
if e.errno == errno.ESRCH:
|
||||
# os.kill raised an error because the process doesn't exist. Maybe
|
||||
|
||||
Reference in New Issue
Block a user