embedded
runtime_error
ai_generated
true
FreeRTOS:在句柄 0x2000ABCD 上检测到互斥锁死锁,任务 'SensorTask' 无限期阻塞
FreeRTOS: mutex deadlock detected on handle 0x2000ABCD, task 'SensorTask' blocked indefinitely
ID: embedded/freertos-mutex-deadlock-detected
90%修复率
88%置信度
1证据数
2025-01-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| FreeRTOS 10.5.1 | active | — | — | — |
| CMISIS-RTOS2 2.1.3 | active | — | — | — |
| ARM GCC 11.3.1 | active | — | — | — |
根因分析
两个任务持有并等待互斥锁形成循环依赖,或任务尝试获取已持有的互斥锁但未使用递归互斥锁类型。
English
Circular dependency between two tasks holding and waiting for mutexes, or a task attempts to take a mutex it already holds without recursive mutex type.
官方文档
https://www.freertos.org/Documentation/RTOS_book.html解决方案
-
Use recursive mutex (xSemaphoreCreateRecursiveMutex) instead of standard mutex if same task needs to take mutex multiple times. Replace all xSemaphoreTake() with xSemaphoreTakeRecursive().
-
Implement mutex lock ordering: ensure all tasks acquire mutexes in the same global order (e.g., always lock sensor mutex before display mutex). Add assert checks using configUSE_MUTEX_DEADLOCK_DETECTION.
无效尝试
常见但无效的做法:
-
Increase task stack size to avoid blocking
95% 失败
Deadlock is caused by mutex logic, not stack overflow; larger stack does not resolve circular wait.
-
Reduce mutex timeout value to force task to give up
80% 失败
Reducing timeout may cause task to abandon operation prematurely, leading to data corruption or incomplete processing.