From 4800734c78174f4a5e2b1b15ff65dc0e1458a313 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Wed, 8 Jul 2020 12:00:25 +0200 Subject: [PATCH] Send kill to process group (#112) * Send kill to process group * fix lint --- builder/utils.py | 19 +++++++++++++------ setup.py | 2 +- tox.ini | 2 +- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/builder/utils.py b/builder/utils.py index aff2a1ce..2e64509c 100644 --- a/builder/utils.py +++ b/builder/utils.py @@ -2,6 +2,7 @@ from pathlib import Path import os import re +import signal import subprocess import sys from typing import Optional, Dict @@ -42,20 +43,26 @@ def run_command( cmd: str, env: Optional[Dict[str, str]] = None, timeout: Optional[int] = None ) -> None: """Implement subprocess.run but handle timeout different.""" + # pylint: disable=subprocess-popen-preexec-fn process = subprocess.Popen( - cmd, shell=True, stdout=sys.stdout, stderr=sys.stderr, env=env + cmd, + shell=True, + stdout=sys.stdout, + stderr=sys.stderr, + stdin=subprocess.DEVNULL, + env=env, + preexec_fn=os.setsid, ) # Run command and wait try: process.communicate(timeout=timeout) - except subprocess.TimeoutExpired as err: - print(f"Timeout for '{cmd}'", flash=True) - process.kill() - raise err + except Exception: + os.kill(os.getpgid(process.pid), signal.SIGTERM) + raise # Process return code if process.returncode == 0: return - print(f"Command '{cmd}' return error {process.returncode}", flash=True) + raise subprocess.CalledProcessError(process.returncode, cmd) diff --git a/setup.py b/setup.py index 0a49106e..2b035f6a 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup -VERSION = "1.12.1" +VERSION = "1.12.2" setup( name="builder", diff --git a/tox.ini b/tox.ini index f68abc53..20272ad7 100644 --- a/tox.ini +++ b/tox.ini @@ -15,4 +15,4 @@ commands = [testenv:black] commands = - black --target-version py36 --check builder setup.py + black --target-version py37 --check builder setup.py