解决 Google TPU 报错 TPU is already in use by another process 锁死修复
排查 Google Cloud TPU VM 上先前训练任务异常退出后,重新运行脚本报 ResourceExhaustedError: TPU device is already in use by another process,分析 libtpu 进程锁与残留死进程清理。
问题现象
重新在 Cloud TPU VM 上提交训练命令时,程序在初始化阶段被拒绝:
RuntimeError: Failed to initialize TPU client: ResourceExhausted: TPU is already in use by another process (PID: 14201). Please terminate other processes or reboot the TPU.
触发根因
- 先前的 Python 训练脚本虽然收到了
Ctrl+C或报错终止,但底层的后台 Worker 子线程依然在操作/dev/accel0设备节点; libtpu.so动态库创建的硬件互斥锁文件被残留的僵尸进程持有未释放;- TPU 驱动拒绝多进程并发访问同一个物理加速芯片。
解决步骤
强行杀死所有仍在持有 TPU 设备的孤儿 Python 进程
在 TPU VM 终端执行彻底清理:
sudo pkill -9 -f python
sudo pkill -9 -f train
检查并释放加速器设备节点占用
使用 fuser 检查设备占用:
sudo fuser -k -v /dev/accel*
若底层驱动陷入僵死,通过 gcloud 命令直接对 TPU 芯片执行软重启
无需重建 VM 实例:
gcloud compute tpus tpu-vm restart my-tpu-vm --zone=us-central2-b
验证确认
重新启动训练作业:
TPU 资源秒级成功独占绑定,内存与矩阵计算单元顺利完成初始化,训练顺畅推进。


