# RuntimeError: DataLoader worker (pid 12345) timed out after 300 seconds waiting for data from queue. Consider increasing timeout or reducing batch size.

- **ID:** `pytorch/dataloader-queue-timeout`
- **Domain:** pytorch
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

DataLoader worker processes are stuck waiting for data from the prefetch queue, typically due to slow data loading, deadlock in custom dataset, or insufficient shared memory.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| torch>=1.7 | active | — | — |
| torch<=2.5.1 | active | — | — |

## Workarounds

1. **Increase the timeout parameter in DataLoader. For example: DataLoader(dataset, batch_size=32, num_workers=4, timeout=600) to allow up to 600 seconds.** (85% success)
   ```
   Increase the timeout parameter in DataLoader. For example: DataLoader(dataset, batch_size=32, num_workers=4, timeout=600) to allow up to 600 seconds.
   ```
2. **Reduce batch size or number of workers to lower memory and I/O pressure. For example: DataLoader(dataset, batch_size=16, num_workers=2)** (90% success)
   ```
   Reduce batch size or number of workers to lower memory and I/O pressure. For example: DataLoader(dataset, batch_size=16, num_workers=2)
   ```
3. **Use prefetch_factor=1 in DataLoader to reduce prefetch queue size: DataLoader(dataset, batch_size=32, num_workers=4, prefetch_factor=1)** (75% success)
   ```
   Use prefetch_factor=1 in DataLoader to reduce prefetch queue size: DataLoader(dataset, batch_size=32, num_workers=4, prefetch_factor=1)
   ```

## Dead Ends

- **Increasing the number of workers to speed up loading** — More workers can exacerbate shared memory contention and lead to more timeouts, especially if the bottleneck is I/O or CPU. (70% fail)
- **Setting timeout=0 to disable timeout** — Setting timeout=0 means no timeout, but the worker may hang indefinitely, causing the training to stall. (80% fail)
