Skip to content

Commit fe39a02

Browse files
Further testing for CI hanging
Signed-off-by: Albert van Houten <[email protected]>
1 parent 1cdeccd commit fe39a02

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

.github/workflows/lib-lint-and-test.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,19 @@ jobs:
137137
138138
- name: Unit testing
139139
working-directory: library
140-
run: uv run pytest tests/unit --cov --cov-report=xml
140+
env:
141+
PYTHONDONTWRITEBYTECODE: 1
142+
OMP_NUM_THREADS: 1
143+
MKL_NUM_THREADS: 1
144+
OPENBLAS_NUM_THREADS: 1
145+
run: |
146+
uv run pytest tests/unit \
147+
--timeout=900 \
148+
--timeout-method=thread \
149+
-n auto \
150+
--dist=loadfile \
151+
--cov \
152+
--cov-report=xml
141153
142154
- name: Upload coverage reports to Codecov
143155
uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1

library/pyproject.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ dev = [
6363
"pytest-csv",
6464
"pytest-timeout",
6565
"pytest-mock",
66+
"pytest-xdist",
6667
"dill",
6768
"tifffile",
6869
]
@@ -419,3 +420,11 @@ markers = [
419420
"xpu", # mark tests which require Intel dGPU,
420421
]
421422
python_files = "tests/**/*.py"
423+
timeout = 300
424+
timeout_method = "thread"
425+
# Prevent forking issues in CI
426+
addopts = "--tb=short --strict-markers"
427+
# Force fork start method for multiprocessing to avoid deadlocks
428+
env = [
429+
"PYTHONDONTWRITEBYTECODE=1",
430+
]

library/tests/conftest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# SPDX-License-Identifier: Apache-2.0
33
from __future__ import annotations
44

5+
import multiprocessing
6+
import os
57
from collections import defaultdict
68
from pathlib import Path
79

@@ -22,6 +24,40 @@
2224
from tests.utils import ExportCase2Test
2325

2426

27+
# Configure multiprocessing to use 'spawn' to avoid deadlocks in CI
28+
def pytest_configure(config):
29+
"""Configure pytest to prevent hanging in CI environments."""
30+
try: # noqa: SIM105
31+
# Set multiprocessing start method to 'spawn' to avoid fork issues
32+
multiprocessing.set_start_method("spawn", force=True)
33+
except RuntimeError:
34+
# Already set, ignore
35+
pass
36+
37+
# Limit thread usage to prevent resource contention
38+
os.environ.setdefault("OMP_NUM_THREADS", "1")
39+
os.environ.setdefault("MKL_NUM_THREADS", "1")
40+
os.environ.setdefault("OPENBLAS_NUM_THREADS", "1")
41+
42+
# Set torch to use limited threads
43+
torch.set_num_threads(1)
44+
torch.set_num_interop_threads(1)
45+
46+
47+
@pytest.fixture(autouse=True)
48+
def cleanup_after_test():
49+
"""Clean up resources after each test to prevent hanging."""
50+
yield
51+
# Force garbage collection
52+
import gc
53+
54+
gc.collect()
55+
# Clear CUDA cache if available
56+
if torch.cuda.is_available():
57+
torch.cuda.empty_cache()
58+
torch.cuda.synchronize()
59+
60+
2561
def pytest_addoption(parser: pytest):
2662
"""Add custom options for perf tests."""
2763
parser.addoption(

library/uv.lock

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)