|
| 1 | +# CI/CD Segmentation Fault Fix |
| 2 | + |
| 3 | +## Problem |
| 4 | +The CI/CD pipeline was experiencing segmentation faults when running tests with pytest-xdist parallel execution. The issue occurred at ~1h 58m into the test run, causing the pipeline to hang indefinitely. |
| 5 | + |
| 6 | +## Root Cause |
| 7 | +The segfault was caused by **nested multiprocessing** conflicts: |
| 8 | + |
| 9 | +1. **pytest-xdist** spawns worker processes for parallel test execution (`-n auto`) |
| 10 | +2. **Performance tests** (`test_cpu_bound_multiprocessing.py`) use `ProcessPoolExecutor` internally |
| 11 | +3. On Linux, the default `fork()` method for multiprocessing can cause issues when combined with pytest-xdist |
| 12 | +4. This created a resource conflict resulting in segmentation faults |
| 13 | + |
| 14 | +## Solution |
| 15 | + |
| 16 | +### 1. Fixed Multiprocessing Start Method (tests/conftest.py) |
| 17 | +Added platform-specific multiprocessing configuration to use `spawn` instead of `fork` on Linux: |
| 18 | + |
| 19 | +```python |
| 20 | +def pytest_configure(config): |
| 21 | + """Configure pytest markers and multiprocessing settings.""" |
| 22 | + import sys |
| 23 | + import multiprocessing |
| 24 | + |
| 25 | + # Fix multiprocessing segfaults on Linux (CI environment) |
| 26 | + if sys.platform == "linux": |
| 27 | + try: |
| 28 | + multiprocessing.set_start_method("spawn", force=True) |
| 29 | + except RuntimeError: |
| 30 | + pass # Method already set |
| 31 | +``` |
| 32 | + |
| 33 | +**Why this works:** |
| 34 | +- `fork()` copies the parent process memory, which can conflict with pytest-xdist's worker processes |
| 35 | +- `spawn()` starts a fresh Python interpreter, avoiding memory conflicts |
| 36 | +- Only applies on Linux where fork is the default (macOS/Windows already use spawn) |
| 37 | + |
| 38 | +### 2. Separated Performance Tests (.github/workflows/ci.yml) |
| 39 | +Moved performance tests to run **serially** (without pytest-xdist) to avoid nested multiprocessing: |
| 40 | + |
| 41 | +```yaml |
| 42 | +- name: Run comprehensive test suite |
| 43 | + run: | |
| 44 | + python -m pytest tests/ \ |
| 45 | + --ignore=tests/performance/ \ |
| 46 | + -n auto \ # Parallel execution for non-performance tests |
| 47 | + ... |
| 48 | +
|
| 49 | +- name: Run performance tests (no parallel execution) |
| 50 | + run: | |
| 51 | + python -m pytest tests/performance/ \ |
| 52 | + --timeout=600 \ # No -n auto flag |
| 53 | + -v \ |
| 54 | + --tb=short |
| 55 | +``` |
| 56 | +
|
| 57 | +### 3. Added Test Marker |
| 58 | +Added `@pytest.mark.no_parallel` marker for tests that use multiprocessing internally: |
| 59 | + |
| 60 | +```python |
| 61 | +@pytest.mark.asyncio |
| 62 | +@pytest.mark.no_parallel |
| 63 | +async def test_gil_contention_demonstration(): |
| 64 | + ... |
| 65 | +``` |
| 66 | + |
| 67 | +## Files Modified |
| 68 | +1. **tests/conftest.py** - Added multiprocessing configuration |
| 69 | +2. **.github/workflows/ci.yml** - Separated performance tests from parallel runs |
| 70 | +3. **tests/performance/test_cpu_bound_multiprocessing.py** - Added `no_parallel` marker |
| 71 | + |
| 72 | +## Benefits |
| 73 | +- ✅ Eliminates segmentation faults in CI/CD |
| 74 | +- ✅ Maintains parallel execution for regular tests (faster) |
| 75 | +- ✅ Runs performance tests safely in serial mode |
| 76 | +- ✅ Platform-specific fix (only applies on Linux) |
| 77 | +- ✅ No performance regression for non-performance tests |
| 78 | + |
| 79 | +## Testing |
| 80 | +To test locally: |
| 81 | + |
| 82 | +```bash |
| 83 | +# Run regular tests in parallel (should work) |
| 84 | +pytest tests/ --ignore=tests/performance/ -n auto -v |
| 85 | +
|
| 86 | +# Run performance tests serially (should work) |
| 87 | +pytest tests/performance/ -v |
| 88 | +
|
| 89 | +# Run all tests |
| 90 | +pytest tests/ -v |
| 91 | +``` |
| 92 | + |
| 93 | +## Expected CI/CD Behavior |
| 94 | +- Regular tests: Run in parallel with pytest-xdist (~10-15 minutes) |
| 95 | +- Performance tests: Run serially after regular tests (~5-10 minutes) |
| 96 | +- Total time: ~20-25 minutes (down from 2+ hours with hangs) |
| 97 | +- No more segmentation faults ✅ |
| 98 | + |
| 99 | +## References |
| 100 | +- Python multiprocessing start methods: https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods |
| 101 | +- pytest-xdist documentation: https://pytest-xdist.readthedocs.io/ |
| 102 | +- Issue: Segmentation fault at 1h 58m 32s into CI run |
0 commit comments