Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make kill process more robustness. #1136

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .github/workflows/test_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on: [pull_request]
jobs:
is_source_changed:
name: Check if sources has changed
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
outputs:
is_source_changed: ${{steps.check_source_change.outputs.changed}}
steps:
Expand All @@ -30,9 +30,9 @@ jobs:
requirements.txt

success:
name: PR Tets Completed
name: PR Test Completed
needs: [is_source_changed, test]
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
if: always() && needs.is_source_changed.result == 'success' && (needs.is_source_changed.outputs.is_source_changed == 'false' || needs.Test.result == 'success')
steps:
- run: echo ${{needs.is_source_changed.result}} ${{needs.is_source_changed.outputs.is_source_changed}} ${{needs.Test.result}}
Expand All @@ -41,7 +41,7 @@ jobs:
lint_python:
name: Run lint on python code
needs: [is_source_changed]
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
if: needs.is_source_changed.outputs.is_source_changed == 'true'
env:
BLACK_VERSION: "22.10.0"
Expand All @@ -64,7 +64,7 @@ jobs:
build_ui:
name: Build UI (with test and lint)
needs: [is_source_changed]
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
if: needs.is_source_changed.outputs.is_source_changed == 'true'
env:
REACT_APP_API_BASE_URL: "/fake/api/endpoint"
Expand Down Expand Up @@ -108,7 +108,7 @@ jobs:
CI: ""
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
os: [ubuntu-22.04, windows-latest]
python-version: [3.7, 3.8, '3.10', 3.11]
fail-fast: false

Expand All @@ -128,10 +128,10 @@ jobs:
name: ui_bundle
path: testplan/web_ui/testing/build
- name: Set up Zookeeper for tests
if: ${{ matrix.os == 'ubuntu-latest' }}
if: ${{ matrix.os == 'ubuntu-22.04' }}
run: sudo apt-get -y install zookeeper zookeeper-bin zookeeperd
- name: Set up Kafka for tests
if: ${{ matrix.os == 'ubuntu-latest' }}
if: ${{ matrix.os == 'ubuntu-22.04' }}
run: |
wget https://archive.apache.org/dist/kafka/3.5.2/kafka_2.13-3.5.2.tgz -O kafka.tgz
sudo mkdir /opt/kafka
Expand Down
1 change: 1 addition & 0 deletions doc/newsfragments/2348_changed.kill_proc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Checks if a process exists by reading the `/proc/<pid>/stat`.
36 changes: 34 additions & 2 deletions testplan/common/utils/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import subprocess
import threading
import time
import re
import warnings
from enum import Enum, auto
from signal import Signals
Expand All @@ -13,7 +14,12 @@
import psutil

from testplan.common.utils.logger import TESTPLAN_LOGGER
from testplan.common.utils.timing import exponential_interval, get_sleeper
from testplan.common.utils.timing import (
exponential_interval,
get_sleeper,
wait,
TimeoutException,
)


def _log_proc(msg: Any, warn=False, output: IO = None):
Expand All @@ -26,6 +32,31 @@ def _log_proc(msg: Any, warn=False, output: IO = None):
warnings.warn(msg)


def process_is_alive(proc_id: int) -> bool:
stat_file_path = f"/proc/{proc_id}/stat"
try:
with open(stat_file_path, "r") as stat_file:
stat_content = stat_file.read()
if re.match(r"\d+\s\(.*\)\s(\S+)\s.+", stat_content).group(1) != "Z":
return False
return True
except:
return True


def wait_process_clean(proc_id: int, timeout: int = 5, output: IO = None):
if platform.system() != "Linux":
return
try:
wait(lambda: process_is_alive(proc_id), timeout=timeout)
except TimeoutException:
_log_proc(
msg=f"Process {proc_id} is not cleaned up",
warn=True,
output=output,
)


def kill_process(
proc: subprocess.Popen,
timeout: int = 5,
Expand Down Expand Up @@ -54,6 +85,7 @@ def kill_process(

retcode = proc.poll()
if retcode is not None:
wait_process_clean(proc.pid, timeout=timeout)
return retcode

child_procs = psutil.Process(proc.pid).children(recursive=True)
Expand Down Expand Up @@ -92,7 +124,7 @@ def kill_process(
msg="While terminating child process - {}".format(exc),
warn=True,
)

wait_process_clean(proc.pid, timeout=timeout)
return proc.returncode


Expand Down