cuda runtime_error ai_generated true

CUDA error: context is destroyed (cudaErrorContextIsDestroyed)

ID: cuda/context-destroyed

Also available as: JSON · Markdown
82%Fix Rate
85%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
12 active

Root Cause

The CUDA context was destroyed while operations were still pending or references to it remained. Happens when cuCtxDestroy is called prematurely, during process forking with active GPU contexts, or when GPU driver resets the context due to a timeout.

generic

Workarounds

  1. 88% success Ensure proper context lifecycle: do not destroy context while operations are pending
    Call cudaDeviceSynchronize() before cuCtxDestroy(). Ensure no async operations are in flight.
  2. 90% success Use spawn instead of fork for multiprocessing to avoid context inheritance issues
    import multiprocessing; multiprocessing.set_start_method('spawn') before creating any CUDA contexts
  3. 80% success Increase TDR timeout on systems with display timeout (Windows) or check nvidia-persistenced (Linux)
    On Linux: systemctl enable nvidia-persistenced. This prevents the driver from destroying idle contexts.

Dead Ends

Common approaches that don't work:

  1. Catch the error and retry the CUDA operation 90% fail

    Once the context is destroyed, all associated allocations, streams, and modules are invalidated; retrying on the same context is impossible

  2. Increase GPU memory to prevent context destruction 85% fail

    Context destruction is not caused by memory pressure; it is caused by explicit destruction or driver timeout

Error Chain

Leads to:
Preceded by:
Frequently confused with: