已合并
fix: add PyObject_ClearWeakRefs in NPU Stream/Event tp_dealloc #41909
fix: add PyObject_ClearWeakRefs in NPU Stream/Event tp_dealloc #41909
已合并
21xiu创建于 7月17日
21xiu
21xiu成员
7月17日

【合入来源】

如有社区issue,请关联issue链接
请勿携带内部流程信息(需求链接、问题单、内部issue等)

【修改方案】

PyTorch 基础类 THPStream / THPEvent 中增加了 weakref 支持(添加了 weakreflist 字段),但 NPU 后端的 tp_dealloc 覆盖函数未同步更新。
CPython 对静态 PyType 不会自动链式调用父类 tp_dealloc,因此 THNPEvent_dealloc 和 THNPStream_dealloc 一直缺少:

  1. PyObject_ClearWeakRefs() — 未清理 weakref,导致 wr_object 悬空指向已释放内存
  2. Py_CLEAR(self->context)(仅 Stream)— lazy stream context 泄漏

【资料变更】

不涉及

【接口变更】

不涉及

【功能验证】

pytest test/dynamo/test_stream.py -k test_npu_stream_event_weakref_callback
修改前:
ScreenShot_20260714192241.JPG
修改后:
ScreenShot_20260714192439.JPG

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 21xiu 的贡献)
21xiu21xiu成员
7月17日 创建了 pull request,commit a977dbad
atomgit-bot
atomgit-bot
7月17日 评论:

变更摘要

此 PR 修复了 NPU 后端 THNPEventTHNPStreamtp_dealloc 覆盖函数中缺失的 weakref 清理逻辑。PyTorch 基础类 THPStream / THPEvent 已增加 weakref 支持,但 NPU 后端的自定义释放函数未同步调用 PyObject_ClearWeakRefs,导致 weakref 中的 wr_object 在对象释放后仍指向已释放内存(悬空指针),引发 use-after-free。同时 THNPStream_dealloc 还缺少 Py_CLEAR(self->context) 导致 lazy stream context 泄漏。改动在两处 tp_dealloc 中添加了必要的清理调用,并补充了回归测试。

主要改动

  • THNPStream_dealloc 中添加 weakref 清理和 context 释放:在 torch_npu/csrc/npu/Stream.cppTHNPStream_dealloc 函数中,析构 NPUStream 之后、调用 tp_free 之前,新增 PyObject_ClearWeakRefs((PyObject*)self) 清除弱引用,以及 Py_CLEAR(self->context) 释放 lazy stream context,防止内存泄漏和悬空指针。

  • THNPEvent_dealloc 中添加 weakref 清理:在 torch_npu/csrc/npu/Event.cppTHNPEvent_dealloc 函数中,析构 NPUEvent 之后、调用 tp_free 之前,新增 PyObject_ClearWeakRefs((PyObject*)self) 清除弱引用。

  • 新增 weakref 回调与悬空指针回归测试:在 test/dynamo/test_stream.py 中添加 _assert_weakref_callback_fires 辅助方法以及两个测试用例 test_npu_stream_event_weakref_callbacktest_dynamo_registry_no_dangling_weakref,分别验证 weakref 回调正确触发以及 dynamo 外部对象注册表中不会保留指向已释放 Stream 的悬空 weakref。

likedislike
atomgit-bot
atomgit-bot
7月17日 评论:

代码审查

审查总结

已审查文件:

  • test/dynamo/test_stream.py — 无问题(测试代码逻辑正确)
  • torch_npu/csrc/npu/Event.cpp — 1 个 P3 问题(注释错误)
  • torch_npu/csrc/npu/Stream.cpp — 无问题

按优先级统计:

  • P0: 0
  • P1: 0
  • P2: 0
  • P3: 1

整体风险评估: 低风险。本次变更的核心修复(在 tp_dealloc 中调用 PyObject_ClearWeakRefs 解决 UAF、在 Stream 中添加 Py_CLEAR(self->context) 解决泄漏)均正确,操作顺序合理,测试覆盖充分。唯一的问题是 Event.cpp 中 tp_weaklistoffset 的注释因复制粘贴错误引用了错误的基类名称,但不影响运行时行为。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
ascend-robotascend-robot成员
7月17日 添加了label:stat/needs-squash
ascend-robotascend-robot成员
7月17日 添加了label:ascend-cla/yes
ascend-robotascend-robot成员
7月17日 添加了label:needs-issue
ascend-robot
ascend-robot成员
7月17日 评论:

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


PR Approval Progress

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

Module Approval Details

module lgtm status approve status
repo-Ascend/pytorch htchu, liangsongwei, ffmh (3/2) htchu, liangsongwei (2/1)
test htchu, liangsongwei, ffmh (3/2) htchu, liangsongwei (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
ascend-robot
ascend-robot成员
7月17日 评论:

Linking Issue Notice

@xiu_21 , the pull request must be linked to at least one issue.
If an issue has already been linked, but the needs-issue label remains, you can remove the label by commenting /check-issue .

likedislike
ascend-robot
ascend-robot成员
7月17日 评论:

当前仓库存在以下 保护分支

Protected Branch Version Release
master
v2.7.1
v2.9.0
v2.10.0
v2.11.0
v2.12.0
v2.9.0-26.1.0
v2.7.1-26.1.0
v2.10.0-26.1.0
v2.11.0-26.1.0
v2.12.0-26.1.0
ci-test

评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作

注意:

  1. /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
  2. 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭
likedislike
ascend-robotascend-robot成员
7月17日 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
7月17日 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
7月17日 添加了label:docs-ci-pipeline-running
21xiu21xiu成员
7月17日 关联了issue:[Bug]: stream/event执行dealloc的时候没有清除weakref
ascend-robotascend-robot成员
7月17日 删除了label:needs-issue
ascend-robot
ascend-robot成员
7月17日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
7月17日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
7月17日 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
7月17日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
7月17日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
7月17日 评论:
流水线 PR-pipeline_pytorch#46172 [ commitID:ced4a3b0 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_LibTorch_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
CodeCheck_lintrunner >>>
开源片段检查 SCA >>>
开发者测试 UT_X86_Part_01 🛑 >>>
UT_X86_Part_02 🛑 >>>
UT_ARM_A3_Part_01 🛑 >>>
UT_ARM_A3_Part_02 🛑 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
UT_ARM_A2_Part_03 >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_DIST_ARM_Part_01 🛑 >>>
UT_DIST_ARM_Part_02 🛑 >>>
UT_DIST_ARM_Part_03 🛑 >>>
UT_DIST_ARM_Part_04 🛑 >>>
UT_ARM_A2_Select_Part_01 >>>
UT_ARM_A2_Select_Part_02 >>>
流水线 PR-pipeline_pytorch >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916, hhz0, LiNuohang, taohuoquan]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
htchu成员
7月17日 评论:

/approve

likedislike
ascend-robotascend-robot成员
7月17日 添加了label:approved
梁松伟
梁松伟成员
7月17日 评论:

/approve

likedislike
梁松伟
梁松伟成员
7月17日 评论:

/check-pr

likedislike
21xiu
21xiu成员
7月17日 评论:

/check-pr

likedislike
ascend-robot
ascend-robot成员
7月17日 评论:

The following label is not ready.

lgtm: Please wait for reviewers to review the code.

likedislike
ffmh
ffmh成员
7月17日 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
7月17日 添加了label:lgtm
ascend-robotascend-robot成员
7月17日 合入了pull request
ascend-robot
ascend-robot成员
7月17日 评论:
流水线 pytorch_gitcode_PR_multiVersion#12778 [ commitID:ced4a3b0 ] 已完成
likedislike