# FreeRTOS: 任务'SensorTask'在互斥锁0x20000400上无限阻塞（所有者：'IdleTask'）

- **ID:** `embedded/rtos-hang-on-mutex-lock`
- **领域:** embedded
- **类别:** runtime_error
- **错误码:** `0xDEADLOCK`
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

互斥锁被一个被抢占或休眠的低优先级任务持有，导致在没有正确优先级继承机制的情况下发生优先级反转。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| FreeRTOS v10.4.6 | active | — | — |
| FreeRTOS v11.0.0 | active | — | — |
| CMSIS-RTOS2 v2.1.3 | active | — | — |

## 解决方案

1. ```
   Enable priority inheritance by setting configUSE_MUTEXES and configUSE_PRIORITY_INHERITANCE to 1 in FreeRTOSConfig.h, then use xSemaphoreCreateMutex() instead of binary semaphores.
   ```
2. ```
   Add a timeout to mutex acquisition using xSemaphoreTake(mutex, pdMS_TO_TICKS(100)) and handle failure by releasing and retrying the mutex.
   ```

## 无效尝试

- **Increase the stack size of 'SensorTask' to prevent stack overflow** — The issue is not stack size but mutex ownership; increasing stack does not resolve the priority inversion. (95% 失败率)
- **Disable all interrupts to force task execution** — Disabling interrupts can cause missed real-time deadlines and does not fix the mutex logic; it may worsen system responsiveness. (90% 失败率)
