Why Your ESP32 Task Watchdog Fires When Nothing Looks Stuck
You get a reboot and a message saying the task watchdog got triggered, and the task it names is IDLE0. Your code has no loop that obviously hangs, and you never touched the idle task. So who is not feeding the dog?
The answer is that the watchdog is usually fed by the idle task, and your task is stopping it from running. Nothing is stuck. Something is just too busy, or too important, to let anything else have the CPU.
What the panic actually says
A typical report looks like this:
E (5123) task_wdt: Task watchdog got triggered. The following tasks/users did not reset the watchdog in time:
E (5123) task_wdt: - IDLE0 (CPU 0)
E (5123) task_wdt: Tasks currently running:
E (5123) task_wdt: CPU 0: my_sensor_task
E (5123) task_wdt: CPU 1: IDLE1
Two lists matter. The first is who missed their deadline. The second is who was on the CPU at that moment. Read the second one first: it is your suspect.
The default timeout in ESP-IDF is 5 seconds (CONFIG_ESP_TASK_WDT_TIMEOUT_S), and by default the idle tasks are subscribed to the watchdog. Which cores' idle tasks are watched depends on your sdkconfig, and the Arduino core ships its own defaults, so check yours rather than trusting mine.
Why the idle task is the canary
FreeRTOS runs the idle task only when no other task is ready. It has the lowest priority, 0. If a task at a higher priority never blocks, the idle task never runs, and it cannot reset the watchdog.
That makes the idle task a cheap health check for the whole core: if it runs at all in five seconds, nobody has been hogging the CPU the entire time. It also means the culprit is never the task named in the first list.
The loop that looks innocent
static void sensor_task(void *arg)
{
for (;;) {
if (data_ready_flag) {
process_sample();
data_ready_flag = false;
}
/* no delay: spins forever when no data arrives */
}
}
This polls a flag. When data is slow, the task spins at full speed and never enters the Blocked state. Create it at priority 5 and IDLE0 starves on that core.
A busy loop is not the only way. These all produce the same panic:
- a polling loop with no delay or blocking call
- a long computation (a big CRC, a JSON parse, a framebuffer fill) at a priority above idle
- a spinlock or a retry loop waiting on hardware that never answers
- a task that keeps getting re-readied by a fast queue or notification stream
Detour: why taskYIELD() does not save you
Quick side-path, because this catches people. You might add taskYIELD() to the loop and expect the idle task to get a turn. It does not.
Yielding only hands the CPU to another ready task of equal or higher priority. The idle task is priority 0, so it is never chosen while your task is ready. To let idle run, your task has to actually block: vTaskDelay, a queue receive with a timeout, a semaphore take, or a task notification wait.
Fix 1: block instead of polling
The real fix is to make the task wait for the thing it is polling for:
static void sensor_task(void *arg)
{
for (;;) {
/* sleeps until the ISR or another task notifies us */
if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(1000)) > 0) {
process_sample();
}
}
}
The task now costs nothing while idle, and the idle task runs and feeds the watchdog. The timeout also gives you a place to log "no data for a second", which is more useful than a reboot.
Fix 2: a delay, with one tick gotcha
If you genuinely must poll, delay between checks. Watch the units though:
vTaskDelay(pdMS_TO_TICKS(1)); /* may be 0 ticks! */
vTaskDelay(1); /* always at least one tick */
With a 100 Hz tick (CONFIG_FREERTOS_HZ=100, the ESP-IDF default as far as I know), one tick is 10 ms, so pdMS_TO_TICKS(1) rounds down to 0. A zero-tick delay does not block, and you are back to starving idle. Confirm your tick rate before relying on millisecond conversions.
Fix 3: long work gets split or subscribed
Sometimes the work really is long and cannot be made event-driven. Two options:
- break it into chunks with a
vTaskDelay(1)between them, so idle gets a slice - subscribe the task itself and feed the dog on purpose, which is honest about the fact that it is long-running
#include "esp_task_wdt.h"
static void long_task(void *arg)
{
ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); /* watch this task */
for (;;) {
for (int i = 0; i < CHUNKS; i++) {
do_chunk(i);
esp_task_wdt_reset(); /* prove we are alive */
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
Subscribing is the right tool when you want the watchdog to catch your task hanging. It is the wrong tool for silencing a panic: resetting the dog from a task that is starving idle hides the starvation, and then the WiFi stack and other lower-priority work quietly stop getting CPU time.
Hang on, why is core 0 the usual victim?
On a dual-core ESP32 the WiFi and system tasks mostly live on core 0, and the Arduino loop() runs on core 1. A busy task pinned to core 0 starves idle there and also fights the radio stack, so you see watchdog resets together with odd WiFi behaviour. Pinning your own heavy work to core 1 with xTaskCreatePinnedToCore is a reasonable first experiment, though it is a workaround for placement, not a fix for a loop that never blocks.
A different panic: the interrupt watchdog
If the message says Interrupt wdt timeout on CPU0, that is another mechanism. It fires when interrupts are blocked for too long (a critical section or a slow ISR), not when idle is starved. Same family of symptom, different cause: keep ISRs short and move the work into a task.
A quick way to find the culprit
- Read the "currently running" line in the panic output.
- Find that task's loop and look for a path where it does not block.
- Check its priority against the idle task and the WiFi tasks.
- Add a blocking wait with a timeout, then confirm the reboot is gone by running for longer than the watchdog period under the slowest input you expect.
The last step matters: the bug only shows when the event you poll for is rare, so a quick test with plenty of data will happily pass.