Developer Tools

PyTorch fixes Metal MPS capture SIGSEGV bug

PyTorch's Metal Performance Shaders fix resolves macOS M2 SIGSEGV crash in 4% of runs

Deep Dive

PyTorch's Core Team landed a critical fix for an intermittent SIGSEGV (segmentation fault) in PyTorch's Metal Performance Shaders (MPS) backend, specifically affecting macOS M2 devices. The issue stemmed from a race condition in the Metal capture system where `MPSProfiler::stopCapture(stream)` could tear down device-wide Metal resources while command buffers were still executing and completion handlers were running on Metal's dispatch threads. This only manifested in about 4% of runs on macOS M2 runners (~15% of failures), never on M1 devices.

The fix introduces a drain operation before stopping captures, using `COMMIT_AND_WAIT` to ensure all GPU work completes. This required synchronizing not just the default stream but all pool streams created during capture. The solution also properly handles deferred device-side errors during capture teardown. Two new tests were added to validate the fix, running under `MTL_CAPTURE_ENABLED=1` in trunk's MPS job. The fix is now in PyTorch's trunk and will be validated through ongoing macos-m2-15 runner monitoring.

Key Points
  • Fixed SIGSEGV crash in PyTorch's Metal Performance Shaders on macOS M2 (~4% of runs)
  • Root cause: Race condition in Metal capture teardown while GPU work was still executing
  • Solution: Added synchronization with COMMIT_AND_WAIT before stopping captures and new tests to validate

Why It Matters

Prevents crashes in PyTorch's MPS backend on macOS M2, ensuring stable training/inference for Apple Silicon GPU users.

📬 Get the top 10 AI stories daily