stack Overflow in task pthread

filchr
Posts: 7
Joined: Thu Apr 26, 2018 6:10 pm

stack Overflow in task pthread

Postby filchr » Thu Jun 28, 2018 4:50 pm

Help ! This error is non-deterministic. Sometimes it happens, sometimes not. When it happens, it seems to be right after I read something from the UART and always in the first seconds of the program execution.

You will find my snapshot attachment very descriptive.

The truth is that i surely put too much weight on my esp32 : I use 2 sockets, one of which runs non stop, the bluetooth, a hardware timer which blinks a led, UART communication.
All of these interact through freeRTOS Tasks (using xEventGroup, xQueue) but also through classic pthreads.

UART read (which is where the exception seems to happen) is run by a pthread.

I try to find where exactly in my code did the exception happen, but I cannot.
I checked the CORE DUMP and through menuconfig I demanded to print via UART all messages (Info, Errors, ... everything). But still, I cannot find nothing that determines where the error actually happened. I guess that's because of the errors nature (stack overflow) but again , why is it sometimes happens and sometimes not?

Anyway, if stack is not enough , what do you propose me to do ? I already tried to increase Main Task stack size from 3584 to 7168(since pthreads live inside mother main task) but this didn't help.

Could there be an implication between hardware timer interrupts and UART?
Could there be an implication between hardware timer interrupts and pthread mutex I also use after using UART RX ?

thanks !

EDIT : My impression was wrong. This error not only happens in the beginning of my program.
Attachments
esp32.jpg
esp32.jpg (570.02 KiB) Viewed 11881 times
Last edited by filchr on Fri Jun 29, 2018 7:29 am, edited 1 time in total.

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

Re: stack Overflow in task pthread

Postby kolban » Fri Jun 29, 2018 6:50 am

In your code, are you creating any additional tasks/threads? If so, what stack size are you allocating and/or what statements are you calling to created those tasks/threads?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

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

Re: stack Overflow in task pthread

Postby chegewara » Fri Jun 29, 2018 8:56 am

If its really pthread stack error then you have in menuconfig settings to change pthread stack size. By default its 3kB.

filchr
Posts: 7
Joined: Thu Apr 26, 2018 6:10 pm

Re: stack Overflow in task pthread

Postby filchr » Mon Jul 02, 2018 7:47 am

Code: Select all

// Start TX uart task
xTaskCreate(tx_task, "uart_tx_task", 1024*2, NULL, configMAX_PRIORITIES-1, NULL);
// Start bluetooth task
xTaskCreate(bt_app_gap_start_up, "connected_to_wifi_program", 2048, NULL, 5, NULL);

// Creating pthread for server usage
pthread_t server_thread;
if (pthread_create(&server_thread, NULL, servering, NULL))
     ESP_LOGE(USER_TAG, "###|COULD NOT start server thread.");

// First intilialize the mutex between the thread & the main thread.
// A socket is shared between rxUART_thread & main thread.
//   so, I need a mutex, so that they don't access it at the same time
if (pthread_mutex_init(&lockSocket, NULL) != 0)
     ESP_LOGE(USER_TAG, "Could not initialize mutex");

//creating pthread for rx uart
pthread_t rxUART_thread;
if (pthread_create(&rxUART_thread, NULL, rx_thread, NULL))
     ESP_LOGE(USER_TAG, "###|COULD NOT start server thread.");
The reason I use pthreads sometimes instead of taks is that C++ exceptions are needed and they don't seem to be supported by a non-main task.

I tried extending the stack using pthread_attr_setStackSize but then I realized that all pthread_attr are disabled for the pthread implementation of ESP32.
But yes ! As chegewara is saying we can extend the pthread task stack size by menuconfig !
Up untill now, the error did not repeat itself thankfully. :) :) :)

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

Re: stack Overflow in task pthread

Postby kolban » Mon Jul 02, 2018 2:41 pm

See also the following issue:

https://github.com/espressif/esp-idf/issues/988

While the make menuconfig seems to allow us to specify a stack size for all new pthreads, this doesn't appear to offer granularity on the amount of stack for any given thread. The issue listed above seems to point to a new function called esp_pthread_set_cfg() which can be used to set the stack size for the next pthread to be created ... this might allow one to tune the stack size on a per pthread creation basis.

I'd also like to mention that C++ exceptions do seem to work for me ... but one has to explicitly enable them in make menuconfig. C++ exceptions handling is off by default.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

filchr
Posts: 7
Joined: Thu Apr 26, 2018 6:10 pm

Re: stack Overflow in task pthread

Postby filchr » Mon Jul 02, 2018 4:30 pm

kolban , thanks for the info !

As far as C++ exceptions are concerned, are you sure they do work? I mean, in my system they do compile but when it comes to the point of getting them thrown, the core dumps.

I leave a screenshot here, of my code and the errors I am getting for the above issue.
As you can see exception in main thread happens naturally.
Attachments
exceptions_in_tasks.png
exceptions_in_tasks.png (457.36 KiB) Viewed 11779 times

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: stack Overflow in task pthread

Postby ESP_Angus » Tue Jul 03, 2018 12:23 am

filchr wrote: As far as C++ exceptions are concerned, are you sure they do work? I mean, in my system they do compile but when it comes to the point of getting them thrown, the core dumps.

I leave a screenshot here, of my code and the errors I am getting for the above issue.
As you can see exception in main thread happens naturally.
C++ exception support is turned off by default in menuconfig, which means that throwing an exception immediately calls std::terminate which calls abort() (as shown in the log).

This is the item which must be enabled to use C++ exceptions:
http://esp-idf.readthedocs.io/en/latest ... EXCEPTIONS

Once C++ exception support is enabled, you should be able to throw/catch exceptions from any RTOS task or pthread.

Note that turning on C++ exception support will increase the overall stack usage of any C++ code, so if a 3KB pthread stack size was already borderline then it will definitely be too small with exception support enabled. You can increase the default size in menuconfig, or switch to creating tasks with FreeRTOS APIs (and explicit stack size in the function call), or you can call esp_pthread_set_cfg() as mentioned by kolban above.

Who is online

Users browsing this forum: No registered users and 75 guests