Skip to content

Commit

Permalink
fix ResourceWarning on process exit (#2472) (#2617)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma67 authored Aug 7, 2023
1 parent 20701fe commit 099f0d8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/changelog/2472.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ResourceWarning on exit caused by periodic update subprocess
7 changes: 5 additions & 2 deletions src/virtualenv/seed/wheels/periodic_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/seed/wheels/test_periodic_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 099f0d8

Please sign in to comment.