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

- **ID:** `embedded/freertos-mutex-deadlock`
- **Domain:** embedded
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 88%

## Root Cause

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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| FreeRTOS Kernel V10.5.1 | active | — | — |
| FreeRTOS Kernel V11.0.0 | active | — | — |
| STM32Cube_FW_F4_V1.27.1 | active | — | — |

## Workarounds

1. **Implement a consistent lock ordering: always acquire mutexes in the same order (e.g., SensorMutex before ControlMutex) across all tasks.** (90% success)
   ```
   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.** (85% success)
   ```
   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).** (80% success)
   ```
   Use a recursive mutex if a single task may re-acquire the same mutex (e.g., via nested function calls).
   ```

## Dead Ends

- **** — Deadlock is a synchronization issue, not a memory issue; larger stacks don't resolve lock ordering conflicts. (95% fail)
- **** — This hides the symptom but the system remains hung; tasks never progress. (100% fail)
- **** — Binary semaphores don't prevent priority inversion or deadlock; the same circular dependency persists. (85% fail)
