为什么 Git 在同时执行多个实例时会给出错误“资源暂时不可用代码:11”?

问题描述

我有一个自动化测试,尝试使用以下异步 Nim 程序从本地 Git 存储库克隆一堆特定修订。

proc clonespecificRevision(downloadMethod: DownloadMethod,url,downloadDir: string,vcsRevision: Sha1Hash) {.async.} =
  assert vcsRevision != notSetSha1Hash
  display("cloning","revision: " & $vcsRevision,priority = MediumPriority)
  case downloadMethod
  of DownloadMethod.git:
    let downloadDir = downloadDir.quoteShell
    createDir(downloadDir)
    discard await tryDoCmdExAsync("git",@["-C",downloadDir,"init"])
    discard await tryDoCmdExAsync("git","remote","add","origin",url])
    discard await tryDoCmdExAsync("git","fetch","--depth","1",$vcsRevision])
    discard await tryDoCmdExAsync("git","reset","--hard","FETCH_HEAD"])
  of DownloadMethod.hg:
    discard await tryDoCmdExAsync("hg",@["clone","-r",$vcsRevision])

结果是

Downloading /tmp/tlockfile/origins/dep1 using git
    cloning revision: 8b6ce61df05f4e21ad954f9ddb487eda8fb64f41
  Executing git -C /tmp/nimble_94295/_tmptlockfileoriginsdep1_0.1.0_8b6ce61df05f4e21ad954f9ddb487eda8fb64f41 init
Downloading /tmp/tlockfile/origins/dep2 using git
    cloning revision: d1ba2f37a1647ca1ed804dfa0c8c73a7de98201e
  Executing git -C /tmp/nimble_94295/_tmptlockfileoriginsdep2_0.1.0_d1ba2f37a1647ca1ed804dfa0c8c73a7de98201e init
    Error:  Resource temporarily unavailable (code: 11)

使用的Git版本是:

> git --version
git version 2.31.1

该问题仅发生在 Linux 上,我的测试在 Windows 和 macOS 系统上正常通过。这个问题的原因是什么?

解决方法

Linux 上的错误 11 是 EAGAINResource temporarily unavailable。 Linux fork 系统调用可能会产生此错误,因为目前没有足够的资源

什么是资源?它们有很多,但其中一个是内存,另一个是进程。两者都可以“暂时”用完,因为程序(进程)运行了一段时间,然后退出;当他们退出时,他们会释放他们正在使用的资源。另请参阅 Fork fails with "resource temporarily unavailable". Which resource?OSError: [Errno 11] Resource temporarily unavailable. What causes this?fork() failing with Out of memory error(请注意,最后一个显示 errno 12、ENOMEM,但您可以在其他类 Unix 上获得 EAGAIN系统在这里,所以我把它包括在内,即使你特别提到了 Linux)。

请注意,这与 Git 本身无关:如果您使用 fork 达到了进程限制,那么您打算在 {{ 1}} 在子进程中返回。