chore: add timeout for subprocess calls

This commit is contained in:
Xuehai Pan 2026-02-13 20:48:14 +08:00
parent daf26ec762
commit 59e50ed3e6
4 changed files with 20 additions and 13 deletions

View file

@ -29,7 +29,7 @@ repos:
args: [--ignore-case]
files: ^docs/source/spelling_wordlist\.txt$
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.14.14
rev: v0.15.1
hooks:
- id: ruff-check
args: [--fix, --exit-non-zero-on-fix]

View file

@ -25,22 +25,24 @@ __email__ = 'XuehaiPan@pku.edu.cn'
__release__ = False
if not __release__:
import os
import subprocess
from pathlib import Path
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
root_dir = Path(__file__).absolute().parent.parent.parent
try:
prefix, sep, suffix = (
subprocess.check_output( # noqa: S603
[ # noqa: S607
'git',
f'--git-dir={os.path.join(root_dir, ".git")}',
f'--git-dir={root_dir / ".git"}',
'describe',
'--abbrev=7',
],
cwd=root_dir,
stderr=subprocess.DEVNULL,
text=True,
encoding='utf-8',
timeout=120.0,
)
.strip()
.lstrip('v')
@ -56,7 +58,7 @@ if not __release__:
else:
__version__ = prefix
del prefix, sep, suffix
except (OSError, subprocess.CalledProcessError):
except (OSError, RuntimeError, subprocess.SubprocessError):
pass
del os, subprocess, root_dir
del Path, subprocess, root_dir

View file

@ -3241,12 +3241,15 @@ def _parse_cuda_visible_devices( # pylint: disable=too-many-branches,too-many-s
""",
),
],
stderr=subprocess.DEVNULL,
text=True,
encoding='utf-8',
timeout=120.0,
)
.decode('utf-8', errors='replace')
.strip()
.split(',')
)
except subprocess.CalledProcessError:
except subprocess.SubprocessError:
pass
else:
uuids = [

View file

@ -25,22 +25,24 @@ __email__ = 'XuehaiPan@pku.edu.cn'
__release__ = False
if not __release__:
import os
import subprocess
from pathlib import Path
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
root_dir = Path(__file__).absolute().parent.parent
try:
prefix, sep, suffix = (
subprocess.check_output( # noqa: S603
[ # noqa: S607
'git',
f'--git-dir={os.path.join(root_dir, ".git")}',
f'--git-dir={root_dir / ".git"}',
'describe',
'--abbrev=7',
],
cwd=root_dir,
stderr=subprocess.DEVNULL,
text=True,
encoding='utf-8',
timeout=120.0,
)
.strip()
.lstrip('v')
@ -56,10 +58,10 @@ if not __release__:
else:
__version__ = prefix
del prefix, sep, suffix
except (OSError, subprocess.CalledProcessError):
except (OSError, RuntimeError, subprocess.SubprocessError):
pass
del os, subprocess, root_dir
del Path, subprocess, root_dir
# The package `nvidia-ml-py` is not backward compatible over releases. This may