Merged
fix(pyACL): 修复 #1479 和 #1481 报告的 GIL 漏释放 #6580
fix(pyACL): 修复 #1479 和 #1481 报告的 GIL 漏释放 #6580
Merged
wuqifanAdd on 2 days ago
wuqifan
wuqifan
2 days ago

Pull Request

描述

本 PR 修复 #1479 和 #1481 报告的三个 GIL 漏释放入口。

  • #1479 涉及媒体编解码回调。将 VdecCallback 和 VencCallback 的 userData == nullptr 检查移到 PyGILState_Ensure() 之前。
  • #1481 涉及 pyACL 线程入口。将 ThrFunc 的 userData == nullptr 检查移到 PyGILState_Ensure() 之前。

根据审查意见,同步修复 acl.rt.launch_callback() 对应的 LaunchCallFunc,将 userData == nullptr 检查移到 PyGILState_Ensure() 之前。

四个入口在收到空指针时记录错误并直接返回,避免获取 GIL 后提前退出。

变更类型

关联的Issue

如何测试

  1. git diff --check 通过。
  2. 在独立 C++ 程序中链接 CPython 3.14.7,验证四个回调。主线程持有 GIL 时,工作线程收到空指针仍可直接返回。传入有效参数时,回调成功和抛异常两条路径均在持有 GIL 时执行 Python 回调,并在返回前释放 GIL。

核对清单

其他信息

验证使用真实 CPython,并用替身替换 ACL 日志和线程异常记录。未运行完整 CANN 构建或真机测试。

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for wuqifan's contribution.)
wuqifanwuqifan
2 days ago Created pull request, commit a83b1b26
wuqifanwuqifan
2 days ago Associated issue: CANN开发者共建计划--runtime media_vdec.cpp 与 media_venc.cpp 回调函数在 userData 为空早退前未调用 PyGILState_Release 导致 Python GIL 永久死锁,CANN开发者共建计划--runtime python/pyACL/util/thread.cpp 中 ThrFunc 获取 GIL 后在 userData 为空时直接返回导致 GIL 永久泄漏
atomgit-bot
atomgit-bot
2 days ago Comments:

变更摘要

本 PR 修复 #1479 和 #1481 报告的 GIL 漏释放问题:在 VdecCallback、VencCallback 和 ThrFunc 这三个入口中,把 userData 空指针检查从 PyGILState_Ensure() 之后移到之前;这样在参数非法并提前返回时就不会获取 GIL,从而避免 PyGILState_Ensure() 获取后未配对的 PyGILState_Release()。正常的回调与线程执行路径仍保持一次获取、一次释放。

主要改动

  • VdecCallback 空指针检查前移:python/pyACL/media/media_vdec.cpp 中把 userData == nullptr 判断及错误日志移到 PyGILState_Ensure() 之前,空指针分支不再获取 GIL。
  • VencCallback 空指针检查前移:python/pyACL/media/media_venc.cpp 中同样把 userData 判空移到 GIL 获取之前,使参数错误时直接返回而不占用 GIL。
  • ThrFunc 线程入口空指针检查前移:python/pyACL/util/thread.cpp 中把 userData 判空提前,返回 nullptr 的异常分支不再进入 GIL 获取流程。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
2 days ago Comments:

代码审查

审查结论

逐文件确认

  • python/pyACL/media/media_vdec.cpp:本次改动正确。PyGILState_Ensure() 从 27 行前移到 32 行(null 校验之后),空指针分支(28-31)不再获取 GIL;成功路径(58)与 result == nullptr 路径(49)仍各释放一次,取/放配对平衡。未发现新增缺陷。
  • python/pyACL/media/media_venc.cpp:同上,PyGILState_Ensure() 移到 33 行,空指针分支(28-31)不取 GIL,44-56 两条路径各释放一次,未发现新增缺陷。
  • python/pyACL/util/thread.cpp:ThrFunc 的 PyGILState_Ensure() 移到 70 行(null 校验之后),86/94 两处释放保持配对;GetExceptionInfo/pthread_t 相关代码未改动,无新增问题。但发现同类入口 LaunchCallFunc 未同步修复(见下)。

问题统计

  • P0:0 项
  • P1:0 项
  • P2:1 项(python/pyACL/util/thread.cpp:70 — 修复不完整:python/pyACL/rt/rt_synchronize.cpp:132-137 的 LaunchCallFunc 仍是“先 Ensure 再空指针早退”,同一 GIL 永久泄漏/进程死锁风险保留)
  • P3:0 项

整体风险判断:本次 diff 本身是正确且低风险的修复,三处改动语义清晰、GIL 取放配对平衡,可合入。唯一需要注意的是修复范围不闭合——同一委托契约下的 LaunchCallFunc 仍保留原有泄漏,建议在同批次补齐,否则同类问题会在另一入口复现。

类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论


🧩 本次 Review 实际调用自定义 Skill:

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
2 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
2 days ago Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-cann/runtime ✅ 王涛, 卢煜坤 (2/2) ✅ 王涛, 卢煜坤 (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

weixin_46600764, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robotCANN-robotCollaborators
2 days ago Set wangtao43,zhangpengpeng8,yanmingxiang,Reyn52166,ykl999,houyanbao,tingwood as approver
CANN-robotCANN-robotCollaborators
2 days ago Set wangtao43,zhangpengpeng8,yanmingxiang,Reyn52166,ykl999,houyanbao,tingwood as approver
atomgit-bot
atomgit-bot2 days agoStart code review1
python/pyACL/util/thread.cpp
@@ -68,3 +67,4 @@
6867 ACL_APP_LOG(ACL_ERROR, "ThrFunc args error");
6968 return nullptr;
7069 }
70+ PyGILState_STATE state = PyGILState_Ensure();
atomgit-bot
atomgit-bot2 days agoComments:

🟡 Medium Priority

变更点:python/pyACL/util/thread.cpp:70 将 PyGILState_STATE state = PyGILState_Ensure(); 从 ThrFunc 入口移到 if (userData == nullptr) return nullptr; 之后(media_vdec.cpp:32、media_venc.cpp:33 同理),修好了这三处“先抢 GIL 再空指针早退导致 GIL 永不释放”的问题。

同一契约的并行调用点未同步修复(证据):python/pyACL/rt/rt_synchronize.cpp 的 LaunchCallFunc(void* userData):

失败模式:aclrtLaunchCallback 注册的回调线程若以 userData == nullptr 触发一次,该线程持有 GIL 直接退出,之后 Python 主线程及其它线程调用 PyGILState_Ensure() 将永久阻塞,进程级死锁——与本 PR 修复的 #1479/#1481 是同一根因、同一后果。也就是说本 PR 把该委托模式的一半入口改成了新写法(先校验后取 GIL),另一半仍是旧写法,行为/契约出现分叉,且缺陷类别未闭环。

触发条件:注册 aclrtLaunchCallback 后底层以空 userData 调用回调(异常/驱动降级路径),概率不高但一旦发生即进程死锁。

建议:本 PR 只修了 ThrFunc / VdecCallback / VencCallback 三处,请一并修正 python/pyACL/rt/rt_synchronize.cpp:132-137 的 LaunchCallFunc:把 PyGILState_Ensure() 移到 userData == nullptr 校验之后(或在该 return 前补 PyGILState_Release(state)),使全部 GIL 入口保持同一契约,避免修复只覆盖部分回调入口。

likedislike
Incorrect?
wuqifanwuqifan
2 days ago Pre merge succeeded (commit_id: 4f14c525400181ab0ac97fb5dacc4e5bb7f5a1bd)
5 event messages are collapsed here。 See more
wuqifanwuqifan
2 days ago Modify description of pull request
wuqifan
wuqifan
2 days ago Comments:

/ai review

likedislike
atomgit-bot
atomgit-bot
2 days ago Comments:

代码审查

✅ 上次发现的问题已修复,没有新问题。

上次发现的问题已修复

  • python/pyACL/util/thread.cpp:70 fix: 同类回调入口 LaunchCallFunc 仍残留 Ensure 后空指针早退的 GIL 泄漏(本次修复不完整)

🧩 本次 Review 实际调用自定义 Skill:

likedislike
Incorrect?
wuqifan
wuqifan
2 days ago Comments:

/compile

likedislike
wuqifanwuqifan
2 days ago Pre merge succeeded (commit_id: ccf0dd020b7f12ebd69e206deadf29a7cd99a50f)
CANN-robot
CANN-robotCollaborators
2 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
2 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
2 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
2 days ago Added label: ci-pipeline-passed
ykl999ykl999Collaborators
2 days ago Resolved final issue
ykl999
ykl999Collaborators
2 days ago Comments:

/approve
/lgtm

likedislike
CANN-robotCANN-robotCollaborators
2 days ago Added label: approved
wangtao
wangtaoCollaborators
2 days ago Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
2 days ago Added label: lgtm
CANN-robotCANN-robotCollaborators
2 days ago Merged pull request