Skip to content

cpu/esp8266: Fix crashes of the ESP866 when rebooting with or disconnecting from WiFi [backport 2026.01]#22019

Merged
leandrolanzieri merged 2 commits intoRIOT-OS:2026.01-branchfrom
gschorcht:backport/2026.01/cpu/esp_common/fix_freertos_task_delay
Jan 28, 2026
Merged

cpu/esp8266: Fix crashes of the ESP866 when rebooting with or disconnecting from WiFi [backport 2026.01]#22019
leandrolanzieri merged 2 commits intoRIOT-OS:2026.01-branchfrom
gschorcht:backport/2026.01/cpu/esp_common/fix_freertos_task_delay

Conversation

@gschorcht
Copy link
Copy Markdown
Contributor

Backport of #22014

This PR fixes the problem described in issue #21558.

With the ESP8266, vTaskDelay is only called by ieee80211_sta_new_state, with interrupts of all levels disabled, for example as a result of executing esp_wifi_disconnect. vTaskDelay uses ztimer_sleep and if ztimer_sleep is then called with interrupts disabled, it leads to memory corruption for some reason when interrupts are re-enabled afterwards. The only way to avoid this is to re-enable interrupts before calling ztimer_sleep in vTaskDelay.

Since debugging ESP8266 code is very limited and sometimes impossible due to there being only one hardware breakpoint and a lot of closed binary code involved when calling esp_wifi_disconnect, it was not possible to find the reason for the memory corruption. An exception occurs after executing esp_wifi_disconnect in ztimer_handler when
_callback_unlock_mutex is called with a mutex parameter that points to wrong address in IROM.

Therefore, enabling the interrupts in vTaskDelay before calling ztimer_sleep is currently the only way to avoid the exception, even though this is only a hack.

The PR includes a small improvement of the pm_reboot function. Even though esp_wifi_stop is implicitly called later in the restart process by esp_restart, it is cleaner to stop the WiFi interface here before saving the RTT counters.

Testing procedure

Compile and flash the GNRC networking example for any ESP8266 with WiFi enabled:

CFLAGS='-DWIFI_SSID=\"ssid\" -DWIFI_PASS=\"pass\"' USEMODULE='esp_wifi' \
BOARD=esp8266-esp-12x make -j8 -C examples/networking/gnrc/networking flash term

Without the PR, using reboot after connecting to the WiFi AP will lead to an exception. With the PR, it should work.

Issues/PRs references

Fixes #21558

(cherry-picked from commit 0fbea71)

With the ESP8266, this function is only called by
`ieee80211_sta_new_state`, with interrupts of all levels disabled,
for example as a result of executing `esp_wifi_disconnect`.
If `ztimer_sleep` is then called with interrupts disabled, for
some reason this leads to memory corruption when interrupts are
re-enabled afterwards. The only way to avoid this is to re-enable
interrupts before calling `ztimer_sleep`.

Since debugging ESP8266 code is very limited and sometimes impossible
due to there being only one hardware breakpoint and a lot of closed
binary code involved when calling `esp_wifi_disconnect`, it was not
possible to find the reason for the memory corruption. An exception
occurs after executing `esp_wifi_disconnect` in `ztimer_handler` when
`_callback_unlock_mutex` is called with a mutex parameter that points
to irrelevant read-only memory in IROM.

Therefore, enabling the interrupts here is currently the only way to
avoid the exception, even though this is only a hack.
(cherry-picked from commit e2d0e28)

Even though `esp_wifi_stop` is implicitly called later in the restart process by `esp_restart`, it is cleaner to stop the WiFi interface here before saving the RTT counters.
@gschorcht gschorcht requested a review from kaspar030 as a code owner January 28, 2026 09:48
@github-actions github-actions bot added Area: pkg Area: External package ports Platform: ESP Platform: This PR/issue effects ESP-based platforms Area: cpu Area: CPU/MCU ports labels Jan 28, 2026
@gschorcht gschorcht changed the base branch from master to 2026.01-branch January 28, 2026 09:49
@gschorcht gschorcht added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jan 28, 2026
@riot-ci
Copy link
Copy Markdown

riot-ci commented Jan 28, 2026

Murdock results

✔️ PASSED

71746cd cpu/esp8266/periph: call esp_wifi_stop in pm_reboot

Success Failures Total Runtime
11001 0 11001 11m:24s

Artifacts

@leandrolanzieri leandrolanzieri added this pull request to the merge queue Jan 28, 2026
Merged via the queue into RIOT-OS:2026.01-branch with commit 996f7cc Jan 28, 2026
28 checks passed
@gschorcht gschorcht deleted the backport/2026.01/cpu/esp_common/fix_freertos_task_delay branch February 1, 2026 17:53
@leandrolanzieri leandrolanzieri added this to the Release 2026.01 milestone Feb 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: cpu Area: CPU/MCU ports Area: pkg Area: External package ports CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Platform: ESP Platform: This PR/issue effects ESP-based platforms Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

esp8266 crashes on WiFi disconnect

3 participants