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 matches PyTorch DataLoader Hangs Indefinitely with OpenCV due to Fork across every rank in a distributed run and reports which rank failed first, so you act on the initiating node instead of the loudest one.
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 crash log or traceback below and get the real root cause for YOUR run, not this generic entry. No account, no card. Logs are masked at ingress and never saved to account history.
Want 14 days on the Scale plan?
Request an evaluation code. A verified workplace organization activates up to 50 diagnoses a day, alerts, history, and follow-up questions. No credit card or automatic subscription.
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...)`.
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 extensionRoot 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
Evaluate Denpex on your own logs
Request a Scale evaluation code. A verified workplace organization activates 14 days with up to 50 diagnoses a day. Every account keeps its current diagnosis allowance and gets a verification path. No card or automatic subscription.
Don'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.