embedded runtime_error ai_generated true

FreeRTOS:任务'SensorTask'中检测到互斥锁死锁,等待信号量0x2000ABCD,由'ControlTask'持有(已阻塞5000毫秒)

FreeRTOS: Mutex deadlock detected in task 'SensorTask' waiting for semaphore 0x2000ABCD, held by 'ControlTask' (blocked for 5000 ms)

ID: embedded/freertos-mutex-deadlock

其他格式: JSON · Markdown 中文 · English
88%修复率
87%置信度
1证据数
2024-05-20首次发现

版本兼容性

版本状态引入弃用备注
FreeRTOS Kernel V10.5.1 active
FreeRTOS Kernel V11.0.0 active
STM32Cube_FW_F4_V1.27.1 active

根因分析

两个或多个任务无限期等待对方持有的互斥锁,通常由于锁定顺序不正确或在优先级反转场景中缺少超时引起。

English

Two or more tasks are waiting indefinitely for mutexes held by each other, typically due to incorrect locking order or missing timeout in priority inversion scenarios.

generic

官方文档

https://www.freertos.org/Documentation/02-Kernel/02-API-reference/01-Queues/01-xQueueSemaphoreTake

解决方案

  1. Implement a consistent lock ordering: always acquire mutexes in the same order (e.g., SensorMutex before ControlMutex) across all tasks.
  2. Add a timeout to semaphore takes (e.g., 100 ms) and release all held mutexes on timeout, then retry, to break deadlocks.
  3. Use a recursive mutex if a single task may re-acquire the same mutex (e.g., via nested function calls).

无效尝试

常见但无效的做法:

  1. 95% 失败

    Deadlock is a synchronization issue, not a memory issue; larger stacks don't resolve lock ordering conflicts.

  2. 100% 失败

    This hides the symptom but the system remains hung; tasks never progress.

  3. 85% 失败

    Binary semaphores don't prevent priority inversion or deadlock; the same circular dependency persists.