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

其他格式: JSON · Markdown 中文 · English
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.

generic

官方文档

https://www.freertos.org/Documentation/RTOS_book.html

解决方案

  1. Use recursive mutex (xSemaphoreCreateRecursiveMutex) instead of standard mutex if same task needs to take mutex multiple times. Replace all xSemaphoreTake() with xSemaphoreTakeRecursive().
  2. 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.

无效尝试

常见但无效的做法:

  1. Increase task stack size to avoid blocking 95% 失败

    Deadlock is caused by mutex logic, not stack overflow; larger stack does not resolve circular wait.

  2. 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.