Skip to content

fix(lsock): handle initialization failures and cleanup trace resources - #152

Open
yuKing123-king wants to merge 1 commit into
DKapture:mainfrom
yuKing123-king:fix/lsock-handle-init-failures
Open

yuKing123-king wants to merge 1 commit into
DKapture:mainfrom
yuKing123-king:fix/lsock-handle-init-failures

Conversation

@yuKing123-king

Copy link
Copy Markdown
Contributor

修复 lsock 初始化阶段异常未处理和错误路径资源清理不完整的问题。

主要修改:

  • 将 CircleBuf 创建、Trace 初始化和 BPF 加载流程放入 try/catch;
  • 捕获 std::bad_alloc、std::system_error、std::runtime_error 等标准异常;
  • 统一 BPF 对象和 CircleBuf 的错误清理流程;
  • 错误路径显式调用 trace.stop(),确保 trace 线程和 tracing 状态及时恢复。

修复原因:
原实现中 CircleBuf 创建、BPF 初始化等操作可能抛出异常,未捕获时会导致工具直接终止。同时,BPF 初始化失败后如果没有停止 Trace,可能残留 trace 线程或保持 tracing 开启状态。

@github-actions

github-actions Bot commented Sep 17, 2026

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit 76882f8)

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

内存泄漏

错误路径 err_outerr_out_before_trace 中缺少 delete cb。当 CircleBuf 分配成功后发生错误并跳转到 err_out 时,cb 已是有效指针但从未被释放,导致内存泄漏。正常退出路径有 delete cb,但错误路径没有。由于 cb 已初始化为 nullptr,在 err_out_before_trace 处添加 delete cb 对两条路径都是安全的(delete nullptr 是无操作)。PR 的目标是统一错误路径资源清理,但 CircleBuf 的清理被遗漏了。

err_out:
	trace.stop();
err_out_before_trace:
	if (obj)
	{
		lsock_bpf::detach(obj);	 // Detach BPF program

@yuKing123-king
yuKing123-king force-pushed the fix/lsock-handle-init-failures branch from 2f3d7e5 to e7d6a4e Compare September 18, 2026 05:45
@github-actions

Copy link
Copy Markdown

Persistent review updated to latest commit e7d6a4e

Signed-off-by: Wang Yu <wangyu6@uniontech.com>
@yuKing123-king
yuKing123-king force-pushed the fix/lsock-handle-init-failures branch from e7d6a4e to 76882f8 Compare September 18, 2026 06:25
@github-actions

Copy link
Copy Markdown

Persistent review updated to latest commit 76882f8

@yuKing123-king

yuKing123-king commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

PR Reviewer Guide 🔍

(Review updated until commit 76882f8)

Here are some key observations to aid the review process:
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
Recommended focus areas for review

内存泄漏

错误路径 err_outerr_out_before_trace 中缺少 delete cb。当 CircleBuf 分配成功后发生错误并跳转到 err_out 时,cb 已是有效指针但从未被释放,导致内存泄漏。正常退出路径有 delete cb,但错误路径没有。由于 cb 已初始化为 nullptr,在 err_out_before_trace 处添加 delete cb 对两条路径都是安全的(delete nullptr 是无操作)。PR 的目标是统一错误路径资源清理,但 CircleBuf 的清理被遗漏了。

这里代码中err_out:里面是有delete rb的。所以以上结论是ai误判了

err_out:
	trace.stop();
err_out_before_trace:
	if (obj)
	{
		lsock_bpf::detach(obj);	 // Detach BPF program
		lsock_bpf::destroy(obj); // Clean up BPF object
	}
	if (cb)
	{
		delete cb;
	}
	return -1;
}
#endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant