You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python子进程调用git时进程无法退出的排查与解决诉求

Git LFS 子进程挂起无法终止问题排查与临时修复

背景

我正在编写Python程序处理LFS错误信息,因为Bitbucket Server上部分仓库存在缺失的LFS文件。运行git lfs fetch --all的输出难以定位缺失文件,所以开发了这个工具,完成后可能会贡献给git项目。

代码片段

import subprocess

def git_lfs_fetch(repo_dir) -> list:
    timeout_sec = 120
    try:
        completed_process = subprocess.run(
            ["git", "lfs", "fetch", "--all"], check=False, cwd=repo_dir, 
            capture_output=True, text=True, timeout=timeout_sec, shell=False)
        return completed_process.stderr.split('\n')
    except subprocess.TimeoutExpired as ex:
        print(f'ERROR Could not complete "{ex.cmd}" before timeout of {timeout_sec} s!')
        print(ex.stderr)
        return []

问题描述

无规律出现subprocess.run()无法返回的情况,原因是git-lfs进程始终不退出。测试环境中该命令通常几秒就能完成,添加2分钟超时后,Python仍无法终止git子进程,手动终止git-lfs则可以获取预期输出。

环境信息

  • Windows 10 64位
  • Python 3.10.7
  • git version 2.39.0.windows.2(从2.35.2.windows.1升级而来)
  • git-lfs/3.3.0 (GitHub; windows amd64; go 1.19.3; git 77deabdf)(从git-lfs/3.0.2升级而来)

临时修复方案

改用直接调用git-lfs而非通过git调用,超时功能恢复正常:

subprocess.run(["git-lfs", "fetch", "--all"], ...)

诉求

希望查明git-lfs及git无法正常终止的原因并修复,之前在Java、C#调用git时遇到过类似问题,怀疑是git自身的问题。

后续更新

使用strace未获取到有效信息,发现多个ssh进程在等待Bitbucket Server响应;升级版本后问题仍存在,ssh进程挂起,计划联系Atlassian支持,但目前缺乏可靠的复现方式。

内容的提问来源于stack exchange,提问作者cb2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 23:20:33