Page 1 of 1

Stack overflow in task that doesn't run

Posted: Wed Apr 25, 2018 8:07 pm
by chromebin
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);
}

Re: Stack overflow in task that doesn't run

Posted: Wed Apr 25, 2018 8:18 pm
by kolban
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?

Re: Stack overflow in task that doesn't run

Posted: Wed Apr 25, 2018 8:22 pm
by chegewara
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.

Re: Stack overflow in task that doesn't run

Posted: Wed Apr 25, 2018 8:54 pm
by chromebin
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

Re: Stack overflow in task that doesn't run

Posted: Wed Apr 25, 2018 9:13 pm
by fly135
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