pytorch
multiprocessing_error
ai_generated
true
RuntimeError: DataLoader worker (pid(s) 12345) exited unexpectedly: OSError: [Errno 9] Bad file descriptor
ID: pytorch/dataloader-bad-file-descriptor
80%Fix Rate
86%Confidence
3Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2 | active | — | — | — |
Root Cause
DataLoader worker process crashes with bad file descriptor. Usually caused by opening files in __init__ instead of __getitem__, or by fork-related resource sharing issues.
genericWorkarounds
-
92% success Open files lazily in __getitem__ instead of __init__
Store file paths in __init__; open files in __getitem__: with open(self.paths[idx]) as f: data = f.read()
Sources: https://pytorch.org/tutorials/
-
85% success Use multiprocessing start method 'spawn' instead of 'fork'
torch.multiprocessing.set_start_method('spawn') # avoids fork-related fd inheritance issues -
82% success Use persistent_workers=True to keep workers alive across epochs
DataLoader(dataset, num_workers=4, persistent_workers=True) # workers keep their file handles open
Dead Ends
Common approaches that don't work:
-
Set num_workers=0 as a permanent fix
60% fail
Eliminates multiprocessing but makes data loading the bottleneck; training throughput drops significantly
-
Increase ulimit for open files
70% fail
The issue is file handles not being picklable across process fork, not hitting the limit