Skip to content

Commit

Permalink
Send kill to process group (#112)
Browse files Browse the repository at this point in the history
* Send kill to process group

* fix lint
  • Loading branch information
pvizeli authored Jul 8, 2020
1 parent a823ae0 commit 4800734
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
19 changes: 13 additions & 6 deletions builder/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path
import os
import re
import signal
import subprocess
import sys
from typing import Optional, Dict
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

VERSION = "1.12.1"
VERSION = "1.12.2"

setup(
name="builder",
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ commands =

[testenv:black]
commands =
black --target-version py36 --check builder setup.py
black --target-version py37 --check builder setup.py

0 comments on commit 4800734

Please sign in to comment.