From 01fff26fee02eed57b495e798a8f0233f14eb7a7 Mon Sep 17 00:00:00 2001 From: wuzhenghui Date: Fri, 29 May 2026 21:12:40 +0800 Subject: [PATCH] fix(esp_hw_support): fix possible dead lock in deepsleep process --- components/esp_hw_support/sleep_modes.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/components/esp_hw_support/sleep_modes.c b/components/esp_hw_support/sleep_modes.c index 437cb6e5bce..239fbf76cf3 100644 --- a/components/esp_hw_support/sleep_modes.c +++ b/components/esp_hw_support/sleep_modes.c @@ -1248,10 +1248,18 @@ static esp_err_t FORCE_IRAM_ATTR deep_sleep_start(bool allow_sleep_rejection) esp_sync_timekeeping_timers(); + // Must acquire all spinlocks which may be acquired during sleep process before stalling other core, + // otherwise deadlock may occur. + esp_os_enter_critical(&spinlock_rtc_deep_sleep); +#if !CONFIG_FREERTOS_UNICORE + extern portMUX_TYPE rtc_spinlock; + esp_os_enter_critical_safe(&rtc_spinlock); // Maybe acquired from temp_sensor_get_raw_value by phy_close_rf callback + esp_clk_private_lock(); // Maybe acquired from esp_clk_slowclk_cal_set +#endif + /* Disable interrupts and stall another core in case another task writes * to RTC memory while we calculate RTC memory CRC. */ - esp_os_enter_critical(&spinlock_rtc_deep_sleep); esp_ipc_isr_stall_other_cpu(); esp_ipc_isr_stall_pause(); #if CONFIG_ESP_INT_WDT && CONFIG_ESP32_ECO3_CACHE_LOCK_FIX @@ -1348,6 +1356,10 @@ static esp_err_t FORCE_IRAM_ATTR deep_sleep_start(bool allow_sleep_rejection) #endif esp_ipc_isr_stall_resume(); esp_ipc_isr_release_other_cpu(); +#if !CONFIG_FREERTOS_UNICORE + esp_clk_private_unlock(); + esp_os_exit_critical_safe(&rtc_spinlock); +#endif esp_os_exit_critical(&spinlock_rtc_deep_sleep); return err; }