From fd87bbdfe55be3da6754a7fbace37540f55f5cbc Mon Sep 17 00:00:00 2001 From: Reid Mello <30907815+rjmello@users.noreply.github.com> Date: Thu, 1 Aug 2024 11:18:25 -0400 Subject: [PATCH] Make python-exec replace the existing process The `python-exec` CLI command now uses `execvpe` to replace the existing process rather than launch an additional subprocess. This reduces some process complexity and ensures that signals make it to their intended destination. --- compute_endpoint/globus_compute_endpoint/cli.py | 4 ++-- compute_endpoint/tests/unit/test_cli_behavior.py | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/compute_endpoint/globus_compute_endpoint/cli.py b/compute_endpoint/globus_compute_endpoint/cli.py index 4bcda0731..cd79408b1 100644 --- a/compute_endpoint/globus_compute_endpoint/cli.py +++ b/compute_endpoint/globus_compute_endpoint/cli.py @@ -5,10 +5,10 @@ import gzip import json import logging +import os import pathlib import re import shutil -import subprocess import sys import textwrap import uuid @@ -924,7 +924,7 @@ def run_python_executable(ctx: click.Context, module: str): E.g., globus-compute-endpoint python-exec path.to.module --ahoy matey """ - subprocess.run([sys.executable, "-m", module] + ctx.args) + os.execvpe(sys.executable, [sys.executable, "-m", module] + ctx.args, os.environ) def create_or_choose_auth_project(ac: AuthClient) -> str: diff --git a/compute_endpoint/tests/unit/test_cli_behavior.py b/compute_endpoint/tests/unit/test_cli_behavior.py index 783a4f849..08e2a2e88 100644 --- a/compute_endpoint/tests/unit/test_cli_behavior.py +++ b/compute_endpoint/tests/unit/test_cli_behavior.py @@ -808,10 +808,12 @@ def run_cmd(): def test_python_exec(mocker: MockFixture, run_line: t.Callable): - mock_subprocess_run = mocker.patch("subprocess.run") + mock_execvpe = mocker.patch("os.execvpe") run_line("python-exec path.to.module arg --option val") - mock_subprocess_run.assert_called_with( - [sys.executable, "-m", "path.to.module", "arg", "--option", "val"] + mock_execvpe.assert_called_with( + sys.executable, + [sys.executable, "-m", "path.to.module", "arg", "--option", "val"], + os.environ, )