DEBUG.WIKI
PanstarCloud:部署应用,接入 AI,云服务器与 AI 网关
晚安云;香港 CN2 ¥15/月
V.PS:让项目靠近用户,亚太、欧洲、北美 KVM VPS

解决 Git 报错 fatal unable to create thread 线程资源耗尽

分析 Git 执行 clone 或 repack 时报 fatal unable to create thread 的系统线程数超限根因,提供 pack.threads 调优方案。

问题现象

在服务器或 CI/CD 容器中执行 git clone 或 git fetch 时报错退出:

fatal: unable to create thread: Resource temporarily unavailable
fatal: index-pack failed

触发根因

  1. Git 在解压和索引对象(index-pack / repack)时,默认会尝试根据 CPU 核心数启动多线程。
  2. 当前用户的最大线程/进程数(ulimit -u / nproc)已经饱和,操作系统拒绝为 Git 创建新的 pthread 线程。
  3. 容器设置了过低且严格的 pids limit(如 pids.max=100)。

解决步骤与操作命令

步骤一:调大当前用户的最大线程限制(nproc)

在当前会话中临时提高:

ulimit -u 65535

步骤二:硬性限制 Git 索引打包线程数

在全局或单次命令中限制线程数量(例如指定为 1 或 2 个线程):

git config --global pack.threads "2"

在命令行中直接传递参数执行:

git clone --config pack.threads=1 https://github.com/example/large-repo.git

步骤三:检查容器 pids.max 限制

若在 Docker 或 Kubernetes 容器内,放开 Pod 或容器的进程数上限配额。

验证确认

重新执行 Git 克隆操作: Git 以指定线程平稳解压数据对象,成功完成检出,未再触发 unable to create thread。