Pull Request已成功合入, 合并人@CANN-robot
(感谢 sinat_31531339 的贡献)代码审查
审查总结
已审查文件: scripts/package/runtime/set_env.sh — 发现 1 个问题
问题分布:
| 优先级 | 数量 |
|---|---|
| P0 | 0 |
| P1 | 0 |
| P2 | 1 |
| P3 | 0 |
整体风险评估:中等。
变更本身意图正确——为该 source 型脚本补充 #!/bin/bash 声明和 set -e 严格错误处理。但 set -e 缺少保存/恢复机制,在脚本被 source 后会泄漏到调用方 shell,导致交互式会话在任意命令失败时意外退出。脚本内业务逻辑对 set -e 本身是安全的,问题仅在于副作用未清理。建议在脚本末尾根据原始 errexit 状态恢复,或采用函数作用域隔离。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 0 |
| 🟡 建议 | 1 |
💬 仅评论


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-cann/runtime | ✅ 王涛, 卢煜坤 (2/2) | ✅ 王涛, 卢煜坤 (2/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
sinat_31531339, thanks for your pull request. All authors of the commits have signed the CLA. 👍


compile


流水线任务触发成功
任务链接 [0986f794f7d047219cadb4a6ca7f4c82][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| 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-27 22:05:26] CI执行结束


流水线任务触发成功
任务链接 [0884a38013c04a33bcbeb44e2538bdb3][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| 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-27 21:57:26] CI执行结束


🟡 Medium Priority
变更在第 12 行新增了 set -e。该脚本的主要使用方式是通过 source 引入(README 及所有 example 均使用 source <path>/set_env.sh),而非直接执行。
问题链:
新增 set -e → 脚本被 source 到当前 shell → set -e 在当前 shell 中生效 → 脚本结束后 set -e 未恢复 → 当前 shell 后续任何命令返回非零都会导致 shell 退出。
触发条件:用户在交互式终端执行 source /usr/local/Ascend/cann/set_env.sh 后,输入任何会失败的命令(如 ls /nonexistent、grep pattern nofile),shell 会立即退出,可能丢失未保存的工作状态。
说明:脚本内的业务逻辑本身对 set -e 是安全的(函数内的 if 条件分支、|| true 保护、$() 命令替换等均在 set -e 豁免范围内)。问题仅在于未在脚本结束前恢复 -e 选项的原始状态。


感谢审查。P2 建议已采纳并修复(commit f96d2318):
本脚本通常以 source 方式加载,直接 set -e 会泄漏到调用方交互 shell,导致后续任意命令失败即退出会话。修复方式:
- 脚本开头用
case "$-" in *e*)记录调用方原始errexit状态; setenv_main执行完毕后,若调用方原本未开启errexit则set +e恢复,并unset临时变量。
已验证两种场景:
- 调用方原本未开
errexit→ source 后不泄漏; - 调用方原本已开
errexit→ source 后保持开启,不被误关。
脚本内部业务逻辑此前已确认对 set -e 安全,本次仅清理其对调用方的副作用。


compile


流水线任务触发成功
任务链接 [7eb04f14758f4541b01eb0c826950aae][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| 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-28 10:59:38] CI执行结束


/approve
/lgtm


流水线任务触发成功
任务链接 [ea3ac8c0499e4af7a4e6ad061a5a79a5][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| 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-28 10:51:38] CI执行结束


Code Review Summary
变更概要
本 PR 修正了 scripts/package/runtime/set_env.sh 的两个缺陷:
- 新增 shebang
#!/bin/bash - 添加
set -e错误处理机制,并实现了 errexit 状态保存与恢复逻辑
审查结果
✅ 未发现任何问题
优点
- ✅ 补充了缺失的 shebang,使脚本可以作为独立可执行文件运行
- ✅ 添加了
set -e错误处理机制,提高脚本健壮性 - ✅ 精心设计的 errexit 状态保存与恢复机制,避免污染调用方 shell 环境
- ✅ 代码清晰,注释充分,易于维护
- ✅ 向后兼容,不会破坏现有使用方式
- ✅ 变量命名规范,使用
__setenv_前缀避免冲突
代码质量评估
- Shell 脚本编写规范,符合最佳实践
- 考虑了 source 加载场景的特殊性,设计周到
- 错误处理机制完善
结论
LGTM (Looks Good To Me) - 代码质量优秀,强烈建议合入。
本次审查未发现任何必须修改或建议修改的问题。


修正 scripts/package/runtime/set_env.sh 两个缺陷: