Skip to content

Commit

Permalink
Kill the child instead of letting it die
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuald committed Feb 9, 2025
1 parent 901b250 commit 4581ed3
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions pyfrc/test_support/pytest_dist_plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import multiprocessing
import os
import pathlib
import sys
import time

from typing import Type

Expand Down Expand Up @@ -34,7 +36,7 @@ def _enable_faulthandler():

faulthandler.enable()
except Exception as e:
logger.warn("Could not enable faulthandler: %s", e)
logger.warning("Could not enable faulthandler: %s", e)
return

try:
Expand All @@ -46,15 +48,25 @@ def _enable_faulthandler():
return


def _run_test(item_nodeid, config_args, robot_class, robot_file, verbose):
def _run_test(item_nodeid, config_args, robot_class, robot_file, verbose, pipe):
"""This function runs in a subprocess"""
robotpy.logconfig.configure_logging(verbose)
_enable_faulthandler()

# keep the plugin around because it has a reference to the robot
# and we don't want it to die and deadlock
plugin = PyFrcPlugin(robot_class, robot_file, True)

ec = pytest.main(
[item_nodeid, "--no-header", *config_args],
plugins=[PyFrcPlugin(robot_class, robot_file, True)],
plugins=[plugin],
)
sys.exit(ec)

# Don't let the process die, let the parent kill us to avoid
# python interpreter badness
pipe.send(ec)
while True:
time.sleep(100)


def _run_test_in_new_process(
Expand All @@ -69,16 +81,21 @@ def _run_test_in_new_process(
else:
item_nodeid = test_function.nodeid

parent, child = multiprocessing.Pipe()

process = multiprocessing.Process(
target=_run_test,
args=(item_nodeid, config_args, robot_class, robot_file, verbose),
args=(item_nodeid, config_args, robot_class, robot_file, verbose, child),
)
process.start()
process.join()
try:
ec = parent.recv()
finally:
process.kill()

if process.exitcode != 0:
if ec != 0:
pytest.fail(
f"Test failed in subprocess: {item_nodeid} (exit code {process.exitcode})",
f"Test failed in subprocess: {item_nodeid} (exit code {ec})",
pytrace=False,
)

Expand Down

0 comments on commit 4581ed3

Please # to comment.