tests: error on ResourceWarning (#9123)

* tests: error on ResourceWarning

* run-cache: avoid leaking temp files on Windows

* set returncode to popen to avoid ResourceWarning on gc.collect()
This commit is contained in:
skshetry
2023-03-08 15:36:10 +05:45
committed by GitHub
parent f1ea64c4ef
commit 08a50ee36e
3 changed files with 17 additions and 5 deletions
+12 -2
View File
@@ -14,7 +14,14 @@ from dvc.utils import fix_env, is_binary
logger = logging.getLogger(__name__)
def _popen(cmd, **kwargs):
def _suppress_resource_warning(popen: Popen):
"""Sets the returncode to avoid ResourceWarning when popen is garbage collected."""
# only use for daemon processes.
# See https://bugs.python.org/issue38890.
popen.returncode = 0
def _popen(cmd, **kwargs) -> Popen:
prefix = [sys.executable]
if not is_binary():
main_entrypoint = os.path.join(
@@ -40,7 +47,10 @@ def _spawn_windows(cmd, env):
startupinfo = STARTUPINFO()
startupinfo.dwFlags |= STARTF_USESHOWWINDOW
_popen(cmd, env=env, creationflags=creationflags, startupinfo=startupinfo)
popen = _popen(
cmd, env=env, creationflags=creationflags, startupinfo=startupinfo
)
_suppress_resource_warning(popen)
def _spawn_posix(cmd, env):
+3 -3
View File
@@ -1,6 +1,5 @@
import logging
import os
import tempfile
from contextlib import contextmanager
from typing import TYPE_CHECKING, List, Optional, Tuple
@@ -167,9 +166,10 @@ class StageCache:
COMPILED_LOCK_FILE_STAGE_SCHEMA(cache)
path = self._get_cache_path(cache_key, cache_value)
parent = self.repo.cache.local.fs.path.parent(path)
local_fs = self.repo.cache.local.fs
parent = local_fs.path.parent(path)
self.repo.cache.local.makedirs(parent)
tmp = tempfile.NamedTemporaryFile(delete=False, dir=parent).name
tmp = local_fs.path.join(parent, fs.utils.tmp_fname())
assert os.path.exists(parent)
assert os.path.isdir(parent)
dump_yaml(tmp, cache)
+2
View File
@@ -132,6 +132,8 @@ profile = "black"
[tool.pytest.ini_options]
addopts = "-ra --cov-config pyproject.toml --dist worksteal"
filterwarnings = [
"error::ResourceWarning",
"error::pytest.PytestUnraisableExceptionWarning",
"error::pytest_mock.PytestMockWarning",
# remove when aiobotocore supports botocore>=1.29.13
"ignore:'cgi' is deprecated and slated for removal in Python 3.13:DeprecationWarning",