PyTorch DataLoader Hangs Indefinitely with OpenCV due to Fork
OpenCV's internal multi-threading uses OpenMP/pthreads. When PyTorch DataLoader uses `num_workers > 0` with the `fork` start method, the child processes inherit a corrupted state of locks held by threads in the parent process, leading to a deadlock.
OpenCV's internal multi-threading uses OpenMP/pthreads.
- Root cause
- OpenCV's internal multi-threading uses OpenMP/pthreads. When PyTorch DataLoader uses `num_workers > 0` with the `fork` start method, the child processes inherit a corrupted state of locks held by threads in the parent process, leading to a deadlock.
- Recommended fix
- Disable OpenCV multithreading import cv2 cv2.setNumThreads(0) Prevents OpenCV from spinning up threads in the parent process that create lock states that break upon forking.
- How Denpex helps
- Denpex investigates PyTorch DataLoader Hangs Indefinitely with OpenCV due to Fork using the evidence you provide or your connected workload collects. Earlier rank, host or application evidence is needed to distinguish an initiating failure from a downstream report.
What this failure is
PyTorch DataLoader Hangs Indefinitely with OpenCV due to Fork is a Software failure seen during ML training runs. OpenCV's internal multi-threading uses OpenMP/pthreads. When PyTorch DataLoader uses `num_workers > 0` with the `fork` start method, the child processes inherit a corrupted state of locks held by threads in the parent process, leading to a deadlock. Common tags: DataLoader Deadlock.
Is this what broke your run? Paste your log.
You're reading about PyTorch DataLoader Hangs Indefinitely with OpenCV due to Fork. Paste your own traceback and relevant evidence for an investigation of your workload, with a next action or a specific missing fact. A reference entry does not establish your cause. No account or card for the free diagnosis. Review data handling before submitting sensitive logs.
Before uploading, review cloud data handling and local options.
Upload your log file. Large files are processed in your browser. Only selected evidence is submitted when you run diagnosis.
Why it happens (the mechanism)
The application just hangs. There is no timeout, no stack trace, and standard debugging tools don't show the C-level lock state easily.
What you'll observe
- Training freezes at epoch start or mid-epoch
- No CPU usage, no GPU usage, 0 errors thrown
Common symptoms and what they mean
| Symptom | Why it happens |
|---|---|
| Process hangs indefinitely during enumeration of DataLoader | OpenCV's internal multi-threading uses OpenMP/pthreads. When PyTorch DataLoader uses `num_workers > 0` with the `fork` start method, the child processes inherit a corrupted state of locks held by threads in the parent process, leading to a deadlock. |
| GPU utilization drops to 0% | OpenCV's internal multi-threading uses OpenMP/pthreads. When PyTorch DataLoader uses `num_workers > 0` with the `fork` start method, the child processes inherit a corrupted state of locks held by threads in the parent process, leading to a deadlock. |
| No crash logs or stack traces are emitted | OpenCV's internal multi-threading uses OpenMP/pthreads. When PyTorch DataLoader uses `num_workers > 0` with the `fork` start method, the child processes inherit a corrupted state of locks held by threads in the parent process, leading to a deadlock. |
Which systems are affected
- PyTorch
- OpenCV
- Multiprocessing
How to confirm this is the problem
Use this checklist to test the hypothesis against a small reproduction. No single line proves the root cause, so preserve the preceding events and compare one variable at a time.
- ✓Run with num_workers=0. If it doesn't hang, it's a multiprocessing issue.
- ✓Use strace on the stuck process: `strace -p <pid>` often shows it stuck in `futex(..., FUTEX_WAIT_PRIVATE...)`.
Root cause
- OpenCV's internal multi-threading uses OpenMP/pthreads. When PyTorch DataLoader uses `num_workers > 0` with the `fork` start method, the child processes inherit a corrupted state of locks held by threads in the parent process, leading to a deadlock.
The fix and how to prevent it
The fix and the prevention pattern
The root cause is on this page and stays free. A free account adds the exact remediation steps, saved history, and the fix on every entry in the encyclopedia.
Sign up free. Unlock the full analysisNo credit card. Daily allowance follows verified trust tier. Instant access.
Diagnose this failure in VS Code
Select the traceback or open the failed terminal, then run Denpex locally to see the initiating rank, collateral failures, exact fix, and verification command without uploading the log.
Install the free VS Code extensionDon't just read the fix, diagnose your run
The encyclopedia tells you what went wrong. Denpex tells you what went wrong in YOUR training run. With your logs, your config, and your stack.