From 030741c315690b56faf21d40a9cb0eaa628cca2c Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sun, 12 Apr 2026 21:25:58 +0200 Subject: [PATCH] Fix pytest OOM: run packages sequentially in separate subprocesses Each python_pkg subpackage now runs in its own pytest subprocess so memory is freed between packages. Prevents 4GB+ accumulation when matplotlib, geopandas, pygame, etc. all load in a single process. --- scripts/pytest_changed_packages.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/scripts/pytest_changed_packages.py b/scripts/pytest_changed_packages.py index f9ceb36..e2c0ae8 100755 --- a/scripts/pytest_changed_packages.py +++ b/scripts/pytest_changed_packages.py @@ -11,7 +11,7 @@ all tests are run as a fallback. from __future__ import annotations -from pathlib import PurePosixPath +from pathlib import Path, PurePosixPath import subprocess import sys @@ -73,9 +73,27 @@ def main() -> int: return 0 packages = _affected_packages(files) - cmd = _build_pytest_command(packages) - result = subprocess.run(cmd, check=False) - return result.returncode + + # When many packages are affected, run each one in a separate subprocess + # to avoid accumulating memory across all test suites (OOM prevention). + if packages is None: + # Discover all subpackages that have a tests/ directory. + packages = { + entry.name + for entry in Path("python_pkg").iterdir() + if (entry / "tests").is_dir() + } + + if not packages: + return 0 + + # Run each package in its own subprocess so memory is freed between runs. + for pkg in sorted(packages): + cmd = _build_pytest_command({pkg}) + result = subprocess.run(cmd, check=False) + if result.returncode != 0: + return result.returncode + return 0 if __name__ == "__main__":