Closed
`start_thread` 创建失败时泄漏 Python 引用 #1137
KaranocaVeAdd on  Oct 18Closed at  22 days ago
KaranocaVe
KaranocaVe
Oct 18 Create

start_thread 创建失败时泄漏 Python 引用

现象与结论

python/pyACL/util/thread.cpp::WrapStartThread 对 callable 和 args 执行 Py_XINCREF 后分配 PyFuncStruct。若分配失败,当前代码立即返回,两个新增引用未释放。若 pthread_create 返回非零,函数仍把错误码打包返回,但没有 worker 接管或释放控制块及其中的两个引用。反复触发这两类失败会累积对象引用及控制块资源。

证据

源码固定为 cann/runtime commit c06e4cccb34ae5c5b616544250718879927da0d7。在该版本中,pthread_create 的返回值直接赋给 thrErr,之后无论成功与否均执行 PyEval_RestoreThread 并返回 (thrId, thrErr)。ThrFunc 仅在线程入口运行时释放控制块及 callable/args 引用;创建失败时线程入口不会被调用。new (std::nothrow) 失败分支也在 worker 创建之前返回。

修复边界

在分配失败路径释放已增加的两个 Python 引用,再设置内存错误并返回;在恢复 GIL 后,仅当 pthread_create 返回非零时释放两个引用并删除控制块。成功时维持现有所有权转移,由 worker 释放资源。

关联 PR: https://gitcode.com/cann/runtime/merge_requests/5618

likedislike
KaranocaVeKaranocaVe
Oct 18 Associated pull request
KaranocaVeKaranocaVe
Oct 18 Modify description of issue
ykl999
ykl999Collaborators
Oct 18 Comments:

你好,相应pyacl问题我们已收到,将会尽快check相关泄露问题

likedislike
ykl999ykl999Collaborators
Oct 18 Assign ykl999 as responsible person
wuqifan
wuqifan
29 days ago Comments:

您好,感谢对pyacl问题的反馈。经评估,该问题为 PyACL 包装层在失败分支的引用计数处理缺陷;成功路径不受影响,但在反复失败场景下会造成引用累积。该类型问题会在后续一并修复。

likedislike
wuqifanwuqifan
22 days ago Associated pull request
wuqifanwuqifan
22 days ago Associated pull request
CANN-robotCANN-robotCollaborators
22 days ago Close issue