[HUST CSE][components][net][sal] harden socket lifetime and close handling#11383
[HUST CSE][components][net][sal] harden socket lifetime and close handling#11383Telecaster2147 wants to merge 1 commit into
Conversation
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
📌 Code Review Assignment🏷️ Tag: componentsReviewers: Maihuanyi Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-05-11 23:21 CST)
📝 Review Instructions
|
There was a problem hiding this comment.
Pull request overview
This PR aims to harden RT-Thread SAL (Socket Abstraction Layer) socket lifetime management by introducing an explicit lifecycle + reference counting scheme, and by extending SAL utests to exercise close/reuse and close-vs-use race scenarios.
本 PR 旨在通过引入显式生命周期状态与引用计数来增强 RT-Thread SAL(Socket Abstraction Layer)的 socket 生命周期管理,并扩展 SAL utest 覆盖 close/reuse 以及 close 与使用并发竞争等场景。
Changes:
- Add per-socket lifecycle state + refcount + completion, change
sal_get_socket()into “lookup + hold temporary reference”, and introducesal_socket_put().
为每个 socket 增加生命周期状态 + 引用计数 + completion,将sal_get_socket()改为“查找并持有临时引用”,并新增sal_socket_put()。 - Rework close/delete paths to detach descriptors from the global table first, then wait for in-flight refs to drain, and finally recycle objects via a cache.
重构 close/delete 路径:先从全局表摘除,再等待引用释放,最后通过 cache 复用对象。 - Extend SAL utests to verify safe failure after close, double-close behavior, close/use racing, and cache-reuse stress.
扩展 SAL utest:覆盖 close 后继续访问的安全失败、double close、close/use 竞态,以及 cache 复用压力测试。
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| components/net/sal/src/sal_socket.c | Implements socket refcount/state/completion, updates most SAL APIs to put refs, and reworks close/delete + cache reuse logic. |
| components/net/sal/include/sal_low_lvl.h | Extends struct sal_socket with lifetime fields and exposes sal_socket_put(). |
| components/net/sal/impl/proto_mbedtls.c | Updates TLS send/recv callbacks to release SAL socket references. |
| components/net/sal/impl/af_inet_lwip.c | Releases SAL socket reference in lwIP poll path. |
| components/net/sal/impl/af_inet_at.c | Releases SAL socket reference in AT poll path. |
| components/net/utest/tc_sal_socket.c | Adds close/reuse stress and close-vs-getsockname race regression tests. |
| /* The global socket table */ | ||
| static struct sal_socket_table socket_table; | ||
| static struct rt_mutex sal_core_lock; | ||
| static struct sal_socket *sal_socket_cache; | ||
| static rt_bool_t init_ok = RT_FALSE; |
269e46b to
5e9e929
Compare
|
copilot提出的问题都已解决 |
拉取/合并请求描述:(PR description)
[
为什么提交这份PR (why to submit this PR)
当前 SAL socket 路径里,socket 描述符查表、对象使用、以及 close/reuse 之间缺少统一的生命周期约束。原有实现至少存在两类问题:
这类问题集中出现在
components/net/sal/src/sal_socket.c的 socket lookup、delete、close 和多条 API 入口路径中,影响 SAL 的健壮性和并发安全性,值得单独修复。你的解决方案是什么 (what is your solution)
本 PR 对 SAL socket 生命周期做了统一收敛,并补充对应回归测试:
struct sal_socket中增加显式生命周期状态和引用计数;sal_get_socket()改为“查找并持有临时引用”,新增sal_socket_put()负责释放;struct sal_socket的整个期间都成对持有/释放引用;components/net/utest/tc_sal_socket.c,覆盖 close 后继续访问、double close、close/use 竞态、socket cache reuse stress 等回归场景。该修改不改变正常 socket 路径的接口语义,主要把原先不受约束的关闭/复用竞态收敛为明确、可验证的失败返回和生命周期行为。
请提供验证的bsp和config (provide the config and bsp)
BSP:
bsp/qemu-vexpress-a9.config:
bsp/qemu-vexpress-a9/.config做基础编译回归验证;.config基础上追加:examples/utest/configs/default.cfgexamples/utest/configs/components/sal.cfgCONFIG_RT_USING_UTESTCASES=yCONFIG_RT_UTEST_TC_USING_SAL=y]
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up