Stack overflow in task that doesn't run

chromebin
Posts: 77
Joined: Wed Feb 07, 2018 3:53 pm

Stack overflow in task that doesn't run

Postby chromebin » Wed Apr 25, 2018 8:07 pm

I get a stack overflow in a task with the below code. It takes about 20s before this happens. Note that await_app_event() never returns.

I saw many stack overflows in other tasks, and I raised the stacksize memory allowance to resolve these errors, despite these tasks doing very little. The allowance needed seems to be way higher than is used only for a task. Why is that? 2k is enough to run a full app on a regular MCU, and not enough for a task that does... nothing?

The only excuses I can think of are:
- interrupts. But even the few ISRs I have do not use local variables.
- runaway stack. But the task would need to run, not wait for an event.

I once saw a define for the minimal stack size value somewhere, if I remember correctly it was 768B. I'm way above that value here.

So what's the deal here??

Code: Select all

EventBits_t await_app_event(u32 event) { 
    return xEventGroupWaitBits(app_events, event, 1, 0, RTOS_WAIT_FOREVER); 
}

void radio_task() {
    while(1) {
        EventBits_t eventResult = await_app_event(EVENT_PACKET_RECEIVED);
        if (eventResult & EVENT_PACKET_RECEIVED) {
            vTaskDelay(10);
        }
    }
}

void radio_task_init(UBaseType_t priority) {
    xTaskCreate(radio_task, "radio", 2048, NULL, priority, NULL);
}

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Stack overflow in task that doesn't run

Postby kolban » Wed Apr 25, 2018 8:18 pm

If you double the stack size does it take double the amount of time to fail? If you comment out the call to await_app_event, do you still get an overflow? The fact that it appears to take time to fail might be indicative of some form of memory leak.

Are other bits being toggled in the event group other than the one you are waiting on?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

chegewara
Posts: 2237
Joined: Wed Jun 14, 2017 9:00 pm

Re: Stack overflow in task that doesn't run

Postby chegewara » Wed Apr 25, 2018 8:22 pm

I may be wrong, but you have very tight loop. Its possible that idle task dont have enough time to clean stack. And on every while loop you have new variable created.

chromebin
Posts: 77
Joined: Wed Feb 07, 2018 3:53 pm

Re: Stack overflow in task that doesn't run

Postby chromebin » Wed Apr 25, 2018 8:54 pm

kolban wrote:
The fact that it appears to take time to fail might be indicative of some form of memory leak.
Well brain-stormed! I disabled most of the other tasks and it now runs for a few minutes without problem. So definitely something fishy is going on.

This is a ported project, in flux, with lots of small edits intended to make things more readable. A memory leak is entirely possible.

Thanks Kolban

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: Stack overflow in task that doesn't run

Postby fly135 » Wed Apr 25, 2018 9:13 pm

chegewara wrote:I may be wrong, but you have very tight loop. Its possible that idle task dont have enough time to clean stack. And on every while loop you have new variable created.
I didn't think that this kind of code would grow the stack. To prove this point he could simply move the definition of the variable outside the loop. That would solve the problem.

The other issue is why does the loop have no delay when a packet isn't recv'd. No loop should spin without doing any work or waiting on something.

Edit: nevermind the last thing. I see the loop is waiting for an event. :oops:

John A

Who is online

Users browsing this forum: No registered users and 128 guests