You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Valgrind检测FD泄漏时存在泄漏仍返回退出码0的解决方案问询

Valgrind检测FD泄漏无法触发非0退出码的CI适配问题

我正尝试搭建一套CI流程,要求检测到文件描述符(FD)泄漏时直接判定任务失败,以下是简单测试过程:

$ valgrind --quiet --track-fds=yes --error-exitcode=1 ./hello_world
hello world!
$ echo $?
0

$ valgrind --quiet --track-fds=yes --error-exitcode=1 ./hello_world_leak
hello world!
==889092== FILE DESCRIPTORS: 4 open (3 std) at exit.
==889092== Open file descriptor 3: /tmp/vg-test/main.cpp
==889092==    at 0x4B968DB: open (open64.c:48)
==889092==    by 0x109249: main (in /tmp/vg-test/hello_world_leak)
==889092== 
==889092== 
$ echo $?
0

注:--quiet参数会在退出时仅3个标准FD未关闭的情况下抑制输出,该行为符合预期。

从测试结果可以看到,即便配置了--error-exitcode=1且测试程序确实存在FD泄漏,Valgrind的退出码依然为0。

我后续尝试的实现思路是将Valgrind输出写入日志文件,再解析日志检查是否包含FILE DESCRIPTORS字符串,对应测试如下:

$ valgrind --quiet --track-fds=yes --error-exitcode=1 --log-file=/tmp/valgrind_out.log ./hello_world
hello world!
$ cat /tmp/valgrind_out.log
==891904== FILE DESCRIPTORS: 4 open (3 std) at exit.
==891904== Open file descriptor 3: /tmp/valgrind_out.log
==891904==    <inherited from parent>
==891904==

但该方案存在新问题:Valgrind用于写入日志的文件/tmp/valgrind_out.log本身会被判定为退出时处于打开状态!如果通过tee重定向Valgrind的stderr输出到文件也会出现同样问题。

目前我能想到的唯一可行方案是解析输出内容,校验退出时打开的FD是否恰好为4个(含3个标准FD),想咨询是否存在更简洁优雅的解决方案?

补充说明:我已经编写了实现上述解析逻辑的包装脚本,仍期待更优雅的实现方案。


内容的提问来源于stack exchange,提问作者Roman Strakhov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 15:15:32