# 运行时错误：DataLoader工作进程（pid 12345）收到信号11（段错误）。可能原因：共享内存耗尽或数据集内存损坏

- **ID:** `pytorch/dataloader-worker-segfault`
- **领域:** pytorch
- **类别:** system_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

DataLoader工作进程因段错误崩溃，通常由于共享内存不足（Linux上/dev/shm太小）或数据集存在内存损坏错误（例如使用不可pickle对象的不安全多进程）。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| pytorch>=1.13 | active | — | — |
| torchvision>=0.14 | active | — | — |
| cuda>=11.7 | active | — | — |
| python>=3.8 | active | — | — |

## 解决方案

1. ```
   Increase shared memory size: In Docker, add --shm-size=8g; on bare metal, remount /dev/shm with larger size: sudo mount -o remount,size=8G /dev/shm
   ```
2. ```
   Reduce num_workers to 0 or 1 to avoid multiprocessing issues: DataLoader(..., num_workers=0)
   ```
3. ```
   Switch multiprocessing context to 'spawn' (instead of 'fork'): torch.multiprocessing.set_start_method('spawn', force=True)
   ```

## 无效尝试

- **Increasing num_workers to speed up data loading** — More workers consume more shared memory, making the segfault more likely. (90% 失败率)
- **Using pin_memory=True without increasing shared memory** — Pin memory also uses shared memory, exacerbating the exhaustion. (85% 失败率)
