Skip to content

Conversation

@eatvector
Copy link
Contributor

@eatvector eatvector commented Jul 25, 2025

拉取/合并请求描述:(PR description)

修复rt_thread_detach 无法释放被分离线程所持有mutex的问题,具体可参考:#10542
[

为什么提交这份PR (why to submit this PR)

rt_thread_detach只能由线程A 向另外一个线程B 发起,这会导致rt_mutex_release B线程持有的mutex时,实际上什么也没做。

你的解决方案是什么 (what is your solution)

本次提交在原有互斥锁机制基础上扩展了强制释放功能,通过新增带 is_force 参数的底层 _rt_mutex_release 函数实现两种释放模式:标准模式严格遵循所有权规则,保持与现有 rt_mutex_release 完全相同的逻辑以确保向后兼容;强制模式则绕过所有权检查,使系统能够在必要时(如线程分离场景)安全回收被分离线程持有的锁资源。

请提供验证的bsp和config (provide the config and bsp)

  • BSP:
  • .config:
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/workflows/bsp_buildings.yml 详细请参考链接BSP自查

@eatvector eatvector marked this pull request as draft July 25, 2025 12:17
@github-actions github-actions bot added the Kernel PR has src relate code label Jul 25, 2025
@github-actions
Copy link

github-actions bot commented Jul 25, 2025

📌 Code Review Assignment

🏷️ Tag: kernel

Reviewers: @GorrayLi @ReviewSun @hamburger-os @lianux-mm @wdfk-prog @xu18838022837

Changed Files (Click to expand)
  • src/ipc.c
  • src/thread.c

📊 Current Review Status (Last Updated: 2025-08-05 10:04 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@eatvector eatvector force-pushed the fix/mutex-release-on-detach branch 13 times, most recently from 9402ae7 to 5faa8d7 Compare July 26, 2025 03:55
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a critical issue in RT-Thread where rt_thread_detach fails to properly release mutexes held by the detached thread. The fix introduces a force release mechanism that bypasses ownership checks to ensure resources are properly cleaned up during thread detachment.

  • Introduces a new rt_mutex_force_release() function that can release mutexes regardless of ownership
  • Refactors existing mutex release logic into a shared internal function with force parameter
  • Updates thread detachment logic to use force release instead of the broken ownership-based approach

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/thread.c Replaces manual mutex state manipulation with proper force release call
src/ipc.c Adds new force release function and refactors existing release logic
include/rtthread.h Exports the new rt_mutex_force_release function

Comment on lines +1598 to +1601
* @warning Forced release (is_force=RT_TRUE) should only be used when:
* The caller is not the mutex owner and the original owner thread
* is guaranteed to be closed (rt_thread_close) and no longer executing any code,
* or the caller is the current mutex owner.
Copy link

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning documentation should specify that forced release bypasses the recursive mutex mechanism, which could lead to undefined behavior if the original owner thread is still running and expects to maintain its hold count.

Suggested change
* @warning Forced release (is_force=RT_TRUE) should only be used when:
* The caller is not the mutex owner and the original owner thread
* is guaranteed to be closed (rt_thread_close) and no longer executing any code,
* or the caller is the current mutex owner.
* @warning Forced release (is_force=RT_TRUE) bypasses the recursive mutex mechanism,
* which could lead to undefined behavior if the original owner thread is still running
* and expects to maintain its hold count. Forced release should only be used when:
* The caller is not the mutex owner and the original owner thread is guaranteed to be closed
* (rt_thread_close) and no longer executing any code, or the caller is the current mutex owner.

Copilot uses AI. Check for mistakes.
Comment on lines +1769 to +1771
* @warning When releasing a mutex not owned by the caller, the original owner thread
* must have been properly terminated via rt_thread_close and must not be
* executing any code at all.
Copy link

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning should also mention that forced release bypasses recursive hold counts and may leave the system in an inconsistent state if the original owner had taken the mutex multiple times recursively.

Suggested change
* @warning When releasing a mutex not owned by the caller, the original owner thread
* must have been properly terminated via rt_thread_close and must not be
* executing any code at all.
* @warning Forced release bypasses recursive hold counts and may leave the system in an inconsistent state
* if the original owner had taken the mutex multiple times recursively. When releasing a mutex not
* owned by the caller, the original owner thread must have been properly terminated via rt_thread_close
* and must not be executing any code at all.

Copilot uses AI. Check for mistakes.
@eatvector eatvector closed this Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Kernel PR has src relate code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant