Pull Request已成功合入, 合并人@CANN-robot
(感谢 mei-feiyao 的贡献)变更摘要
本次 PR 修复了 MstxDataHandler 线程类中因 start_ 标志位设置时机不恰当导致的竞态问题:在 Start() 中先启动线程再设置 start_=true,使得 Run() 方法可能在 start_ 尚未置位时提前退出,而消费线程退出后数据缓冲区持续积压,最终导致 push 失败。修改方案是将 start_ 的设置与清除移入 Run() 方法内部,并将退出条件从 !start_.load() 改为 IsQuit(),确保线程生命周期由框架的启停机制统一控制。
主要改动
start_标志位移入Run()方法管理:在Run()方法循环开始前执行start_.store(true),循环结束后执行start_.store(false),确保标志位与实际线程运行状态严格同步,消除竞态窗口。- 从
Start()和Stop()中移除start_的设置与清除:原Start()中的start_.store(true)和Stop()中的start_.store(false)被删除,避免与Run()中的状态管理冲突。 - 退出条件由
!start_.load()改为IsQuit():Run()内部的循环退出判断不再依赖start_标志,而是使用线程基类提供的IsQuit()方法,使线程退出逻辑与框架的Thread::Stop()机制保持一致。


代码审查
关闭总结
审查文件: src/dfx/msprof/collector/dvvp/msprof/msproftx/mstx/src/mstx_data_handler.cpp — 发现 1 个问题。
按优先级统计:
- P0: 0
- P1: 1(
Stop()竞态条件,析构时可能 use-after-free) - P2: 0
- P3: 0
整体风险评估: 此变更正确修复了原问题(消费线程过早退出的竞态),但引入了反向竞态:Stop() 在新线程尚未执行到 start_.store(true) 的窗口内被调用时,会跳过 Thread::Stop() 直接返回,导致线程泄漏;更严重的是在析构路径中,buffer 被 Uninit() 销毁后线程仍可能访问,造成 use-after-free。建议在 Stop() 中始终调用 Thread::Stop() 以确保线程被正确 join。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 1 |
| 🟡 建议 | 0 |
⛔ 需要修改


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 |
|---|---|---|
| src/dfx | ✅ zhuliangying, chenhao_1209 (2/2) | ✅ zhuliangying (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
mei-feiyao, thanks for your pull request. All authors of the commits have signed the CLA. 👍


compile


流水线任务触发成功
任务链接 [1beb913d9630484893f4732532d8754e][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| Compile_Ascend_X86 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM | ✅ SUCCESS | >>>>> | >>>>> |
| pre_comment | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| UT_Test_acl | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_v201 | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_c | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_david | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_910b | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_common | ✅ SUCCESS | >>>>> | |
| UT_Test_platform | ✅ SUCCESS | >>>>> | |
| UT_Test_qs | ✅ SUCCESS | >>>>> | |
| UT_Test_aicpusd | ✅ SUCCESS | >>>>> | |
| UT_Test_tsd | ✅ SUCCESS | >>>>> | |
| UT_Test_error_manager | ✅ SUCCESS | >>>>> | |
| UT_Test_slog | ✅ SUCCESS | >>>>> | |
| UT_Test_atrace | ✅ SUCCESS | >>>>> | |
| UT_Test_msprof_part1 | ❌ FAILED | ||
| UT_Test_msprof_part2 | ✅ SUCCESS | ||
| UT_Test_adump | ✅ SUCCESS | >>>>> | |
| UT_Test_mmpa | ✅ SUCCESS | >>>>> | |
| UT_Test_camodel_check | ✅ SUCCESS | ||
| API_Check | ✅ SUCCESS | >>>>> | |
| PreSmoke_A900_npupool | ✅ SUCCESS | >>>>> |


流水线任务触发成功
任务链接 [b653964060bc4474a1325fa037ff8713][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| codecheck | ✅ SUCCESS | >>>>> | |
| SCA | ✅ SUCCESS | >>>>> | |
| antipoison | ✅ SUCCESS | >>>>> | |
| codecheck_Check_Pr | ✅ SUCCESS | ||
| codecheck_style | ✅ SUCCESS | >>>>> | |
| codecheck_precommit | ✅ SUCCESS | >>>>> | |
| StaticCheck_codespell | ✅ SUCCESS | ||
| StaticCheck_link_validity | ✅ SUCCESS | ||
| StaticCheck_resource_existence | ✅ SUCCESS | ||
| StaticCheck_tag_closed | ✅ SUCCESS | ||
| StaticCheck_markdownlint | ✅ SUCCESS |
[2026-07-29 18:11:19] CI执行结束


🟠 High Priority
此变更将 start_ 的写入从 Start()/Stop() 移到了 Run() 内部。Start() 调用 Thread::Start() 创建线程后,start_ 仍为 false,直到新线程进入 Run() 才设为 true。在此期间存在一个竞态窗口:
竞态窗口:Thread::Start() 已返回(线程已创建),但 Run() 尚未执行到 start_.store(true)。
在此窗口内:
- 若调用
Stop():start_为false→Stop()直接返回PROFILING_SUCCESS,不调用Thread::Stop(),线程未被停止、未被 join。 - 若通过析构函数路径(
~MstxDataHandler()→Uninit()→Stop()→mstxDataBuf_.UnInit()):buffer 被反初始化后,线程才进入Run(),此时quit_仍为false(Thread::Stop()未被调用),线程将进入循环并调用ReportData()访问已销毁的mstxDataBuf_,造成 use-after-free。
Thread::~Thread() 虽然也会调用 Thread::Stop() 来 join 线程,但它发生在 MstxDataHandler::~MstxDataHandler() 之后(C++ 析构顺序:派生类先、基类后),此时 buffer 已被 Uninit() 销毁,线程可能在基类析构 join 之前就已经访问了已销毁的 buffer。
|
78 | + int MstxDataHandler::Stop() |
|
79 | + { |
|
80 | + analysis::dvvp::common::thread::Thread::Stop(); |
|
81 | + Flush(); |
|
82 | + return PROFILING_SUCCESS; |
| 78
83 | } |


compile


流水线任务触发成功
任务链接 [fda0def4bca34d6183f7081f2076e58a][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| Compile_Ascend_X86 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM | ✅ SUCCESS | >>>>> | >>>>> |
| pre_comment | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| UT_Test_acl | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_v201 | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_c | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_david | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_910b | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_common | ✅ SUCCESS | >>>>> | |
| UT_Test_platform | ✅ SUCCESS | >>>>> | |
| UT_Test_qs | ✅ SUCCESS | >>>>> | |
| UT_Test_aicpusd | ✅ SUCCESS | >>>>> | |
| UT_Test_tsd | ✅ SUCCESS | >>>>> | |
| UT_Test_error_manager | ✅ SUCCESS | >>>>> | |
| UT_Test_slog | ✅ SUCCESS | >>>>> | |
| UT_Test_atrace | ✅ SUCCESS | >>>>> | |
| UT_Test_msprof_part1 | ✅ SUCCESS | ||
| UT_Test_msprof_part2 | ✅ SUCCESS | ||
| UT_Test_adump | ✅ SUCCESS | >>>>> | |
| UT_Test_mmpa | ✅ SUCCESS | >>>>> | |
| UT_Test_camodel_check | ✅ SUCCESS | ||
| API_Check | ✅ SUCCESS | >>>>> | |
| PreSmoke_A900_npupool | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_report | ✅ SUCCESS | >>>>> | |
| UT_Test_msprof_report | ✅ SUCCESS | >>>>> |
[2026-07-30 10:03:44] CI执行结束


流水线任务触发成功
任务链接 [581a56d81acd46f6886b37aa388f352c][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| codecheck | ✅ SUCCESS | >>>>> | |
| SCA | ✅ SUCCESS | >>>>> | |
| antipoison | ✅ SUCCESS | >>>>> | |
| codecheck_Check_Pr | ✅ SUCCESS | ||
| codecheck_style | ✅ SUCCESS | >>>>> | |
| codecheck_precommit | ✅ SUCCESS | >>>>> | |
| StaticCheck_codespell | ✅ SUCCESS | ||
| StaticCheck_link_validity | ✅ SUCCESS | ||
| StaticCheck_resource_existence | ✅ SUCCESS | ||
| StaticCheck_tag_closed | ✅ SUCCESS | ||
| StaticCheck_markdownlint | ✅ SUCCESS |
[2026-07-30 09:55:43] CI执行结束


/approve


The following users do not have permission to comment /lgtm or /approve on any module in this PR:
wangluyao17


/lgtm


The following users do not have permission to comment /lgtm or /approve on any module in this PR:
wangluyao17


Pull Request
描述
请清晰准确地描述本次 Pull Request 的意图和变更内容。
意图:修复mstx数据偶先push失败的问题
原因:mstx_data_handler是线程类,在start阶段先开启线程,再设置start_=true;在stop阶段关闭线程,并设置start_=false;在线程的Run方法(用于消费buffer数据)里,判断start_ != true时就break掉。这里就有可能发生线程start后,start_还未设置为true,那么Run方法里判断start不为true,直接退出,但是并未设置start_ = false。那么用户程序调用mstx接口打点时,数据会尝试往数据buffer里push,因为消费线程已经退了,buffer里的数据一直未消费,那么push数据条数达到buffer容量后就会报错不支持push。
修改:修改逻辑,先设置start_=true,再start线程
变更类型
请选择本次引入的变更类型:
关联的Issue
https://gitcode.com/Ascend/msprof/issues/112
如何测试
描述测试此变更的步骤和前提条件:
1.
2.
核对清单
其他信息
在此添加任何其他关于本次 PR 的说明。