# RuntimeError: CUDA error: operation not permitted when stream is capturing (streamCaptureInvalidated)

- **ID:** `cuda/stream-capture-invalid-scope`
- **Domain:** cuda
- **Category:** runtime_error
- **Error Code:** `cudaErrorStreamCaptureInvalidated`
- **Verification:** ai_generated
- **Fix Rate:** 81%

## Root Cause

A CUDA graph capture is in progress on a stream, but an operation (e.g., memory allocation, host-side sync) that is invalid during capture was attempted, invalidating the capture.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| CUDA 12.0 | active | — | — |
| PyTorch 2.1.0 | active | — | — |
| NVIDIA Driver 535.129.03 | active | — | — |

## Workarounds

1. **Move all memory allocations and host-device synchronization outside the capture scope. Example: pre-allocate tensors before calling torch.cuda.CUDAGraph.begin_capture(), and use torch.cuda.synchronize() only after capture ends.** (88% success)
   ```
   Move all memory allocations and host-device synchronization outside the capture scope. Example: pre-allocate tensors before calling torch.cuda.CUDAGraph.begin_capture(), and use torch.cuda.synchronize() only after capture ends.
   ```
2. **Use cudaStreamBeginCapture with cudaStreamCaptureModeGlobal to allow more operations, but ensure no host-side blocking calls occur during capture. In PyTorch, wrap the capture in a context manager that defers any print or sleep calls.** (80% success)
   ```
   Use cudaStreamBeginCapture with cudaStreamCaptureModeGlobal to allow more operations, but ensure no host-side blocking calls occur during capture. In PyTorch, wrap the capture in a context manager that defers any print or sleep calls.
   ```

## Dead Ends

- **** — This disables cuDNN heuristics but does not fix the capture violation; the error will reoccur if capture is attempted again. (92% fail)
- **** — Thread configuration is unrelated to capture validity; the error is about operations allowed during capture. (98% fail)
