From fc46f11b6aacde35c299915f77269231a22b9d35 Mon Sep 17 00:00:00 2001 From: sigma67 Date: Fri, 4 Aug 2023 10:59:57 +0200 Subject: [PATCH 1/2] fix ResourceWarning on exit caused by periodic update subprocess (#2472) --- docs/changelog/2472.bugfix.rst | 1 + src/virtualenv/seed/wheels/periodic_update.py | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 docs/changelog/2472.bugfix.rst diff --git a/docs/changelog/2472.bugfix.rst b/docs/changelog/2472.bugfix.rst new file mode 100644 index 000000000..02effe483 --- /dev/null +++ b/docs/changelog/2472.bugfix.rst @@ -0,0 +1 @@ +Fixed ResourceWarning on exit caused by periodic update subprocess diff --git a/src/virtualenv/seed/wheels/periodic_update.py b/src/virtualenv/seed/wheels/periodic_update.py index 7d743540f..3d0239d4d 100644 --- a/src/virtualenv/seed/wheels/periodic_update.py +++ b/src/virtualenv/seed/wheels/periodic_update.py @@ -12,7 +12,7 @@ from itertools import groupby from pathlib import Path from shutil import copy2 -from subprocess import PIPE, Popen +from subprocess import DEVNULL, Popen from textwrap import dedent from threading import Thread from urllib.error import URLError @@ -216,7 +216,7 @@ def trigger_update(distribution, for_py_version, wheel, search_dirs, app_data, e .format(distribution, for_py_version, wheel_path, str(app_data), [str(p) for p in search_dirs], periodic), ] debug = env.get("_VIRTUALENV_PERIODIC_UPDATE_INLINE") == "1" - pipe = None if debug else PIPE + pipe = None if debug else DEVNULL kwargs = {"stdout": pipe, "stderr": pipe} if not debug and sys.platform == "win32": kwargs["creationflags"] = CREATE_NO_WINDOW @@ -230,6 +230,9 @@ def trigger_update(distribution, for_py_version, wheel, search_dirs, app_data, e ) if debug: process.communicate() # on purpose not called to make it a background process + else: + # set the returncode here -> no ResourceWarning on main process exit if the subprocess still runs + process.returncode = 0 def do_update(distribution, for_py_version, embed_filename, app_data, search_dirs, periodic): # noqa: PLR0913 From eef8aa842203e0c6b3d635bfd52b3c7944b9fdac Mon Sep 17 00:00:00 2001 From: sigma67 Date: Sat, 5 Aug 2023 09:05:17 +0200 Subject: [PATCH 2/2] fix test --- tests/unit/seed/wheels/test_periodic_update.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/seed/wheels/test_periodic_update.py b/tests/unit/seed/wheels/test_periodic_update.py index 32931bfe3..85756d93b 100644 --- a/tests/unit/seed/wheels/test_periodic_update.py +++ b/tests/unit/seed/wheels/test_periodic_update.py @@ -316,7 +316,7 @@ def test_trigger_update_no_debug(for_py_version, session_app_data, tmp_path, moc ) assert args == ([sys.executable, "-c", cmd],) - expected = {"stdout": subprocess.PIPE, "stderr": subprocess.PIPE} + expected = {"stdout": subprocess.DEVNULL, "stderr": subprocess.DEVNULL} if sys.platform == "win32": expected["creationflags"] = CREATE_NO_WINDOW assert kwargs == expected