Pthread_create() exceptin

Hello,

i have this function which recieve POSTs from a client including commands to controll the RGB accordenly.

void handleLEDdata(){
  
  // Send the response to the client

  cJSON *json = cJSON_Parse(ethernetServer.arg("plain").c_str());

  int indx =  atoi(cJSON_GetObjectItemCaseSensitive(json, "index")->valuestring);
  String st = cJSON_GetObjectItemCaseSensitive(json, "state")->valuestring;
  uint8_t r = atoi(cJSON_GetObjectItemCaseSensitive(json, "red")->valuestring);
  uint8_t g = atoi(cJSON_GetObjectItemCaseSensitive(json, "green")->valuestring);
  uint8_t b = atoi(cJSON_GetObjectItemCaseSensitive(json, "blue")->valuestring);

  led_state_type state;

  String res = "00";
  for(int i = 0; i < 8; i++){
    if(i == 0) {
		  state.bits.on = strtol(("0000000" + String(st[i])).c_str(), NULL, 2);
	  } else if(i == 1) {
		  state.bits.mode = strtol(("0000000" + String(st[i])).c_str(), NULL, 2);
    }else{
      res += st[i];
    }
  }

  state.bits.res = strtol(res.c_str(), NULL, 2);

  setLEDState(indx, state, r, g, b);

  updateLEDState(indx);

  ethernetServer.send(200, "text/html", "");

}

And this function decide how the RGB should act:

void updateLEDState(int indx) {
  if(ledinfo[indx].state.bits.on == 1 && ledinfo[indx].state.bits.mode == 0){
    continueLED(indx);
  }
  else if(ledinfo[indx].state.bits.on == 1 && ledinfo[indx].state.bits.mode == 1){
    pthread_t tid;
    pthread_create(&tid, NULL, blinkLED, (void *)&indx);

  }
  else{
    offLED(indx);
  }
}

And this is the blink function:

void *blinkLED(void *index){

  int indx = *((int *) index);

  uint32_t color = ledinfo[indx].colorcode_red << 16 | ledinfo[indx].colorcode_green << 8 | ledinfo[indx].colorcode_blue;
  // Check if the tool is present
  //  Tool is present and is the right for next process           Tool is in process and must be inserted
  while(ledinfo[indx].state.bits.on == 1 && ledinfo[indx].state.bits.mode == 1){ 
    for(int i = 0; i < Num_LED; i++){
      ring1.setPixelColor(i, color);
    }
    ring1.show();
    delay(250);
    for(int i = 0; i < Num_LED; i++){
      ring1.setPixelColor(i, 0x000000);
    }
    ring1.show();
    delay(250);
  }
  pthread_exit(NULL);
}

The problem is when the server (ESP32) get a POST from client asking to set the RGB in blink mode then it reboots and nothing happens.
I think the issue is with the pthread.
Here is the output:

Guru Meditation Error: Core  1 panic'ed (Unhandled debug exception). 
Debug exception reason: Stack canary watchpoint triggered (pthread) 
Core  1 register dump:
PC      : 0x4037e45a  PS      : 0x00060c36  A0      : 0x8038064c  A1      : 0x3fcf37e0  
A2      : 0x3fc98160  A3      : 0xffffffff  A4      : 0x00060c23  A5      : 0x000000ff  
A6      : 0x000000ff  A7      : 0x3fc96ad4  A8      : 0x000002cc  A9      : 0x00000001  
A10     : 0x03c98160  A11     : 0x3fc98160  A12     : 0x3fc96ea4  A13     : 0xffffffff  
A14     : 0x00000000  A15     : 0x00000000  SAR     : 0x0000000e  EXCCAUSE: 0x00000001  
EXCVADDR: 0x00000000  LBEG    : 0x400556d5  LEND    : 0x400556e5  LCOUNT  : 0xffffffff 

So please any help.

Post a complete code that compiles and demonstrates the problem.
Run the ESP32 Exception Decoder and post those results at the same time.

Hello,

Does not work by me, idk why
Here is the Backtrace:

0x4037e457:0x3fcf3740 0x40380649:0x3fcf3770 0x4038060d:0x3fcf3790 0x40380756:0x3fcf37b0 0x40377f16:0x3fcf37d0 0x4037ec35:0x3fcf37f0 0x4037e347:0x3fcf3810 0x42014181:0x3fcf3830 0x42013f37:0x3fcf3850 0x4201432e:0x3fcf3870 0x4200d365:0x3fcf3ø€øx€€€xø€€€€€€€x€€€ø€€xø€€x€€€€xx€x€€€x€€€€xø€€€€€€€€€€øø€xxø€€x€€€€xx€x€€€xx€€€xø€€€€€x€x€x€x€øxø€€x€€€€xx€x€€€xx€x€xx€xx€xx€xx€xxx€xx€€€x€øxøxxx€ø€x€xx€€xø€€ø€€ø€€€xx€€€€€x€€€x€€xxxx€øxxø€€x€xx€x€€xx€xxøxøxøx€øxøx€xx€x€xx€xüKH!JçÂÏ‚ÒaÆç†jaÊ@ÊbRÆåý

Don't know what to tell you. It works for everyone on the forum who has tried it.

Not useful.

Decoded:

Decoding stack results
0x4037e457: spi_flash_hal_gpspi_configure_host_io_mode at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/hal/esp32s3/include/hal/gpspi_flash_ll.h line 250
0x40380649: __assert_func at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/soc/include/soc/soc_memory_types.h line 140
0x4038060d: __assert_func at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/newlib/assert.c line 41
0x40380756: _free_r at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/newlib/heap.c line 48
0x40377f16: esp_clk_cpu_freq at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_hw_support/esp_clk.c line 63
0x4037ec35: spi_flash_hal_erase_chip at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/hal/esp32s3/include/hal/spimem_flash_ll.h line 85
0x4037e347: spi_flash_hal_gpspi_configure_host_io_mode at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/hal/esp32s3/include/hal/gpspi_flash_ll.h line 294
0x42014181: ledc_timer_rst at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/driver/ledc.c line 250
0x42013f37: ledc_set_timer_div at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/driver/ledc.c line 317
0x4201432e: ledc_update_duty at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/driver/ledc.c line 647
0x4200d365: fs::File::close() at c:\users\khalilm\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32s3-elf-gcc\esp-2021r2-patch5-8.4.0\xtensa-esp32s3-elf\include\c++\8.4.0\bits/shared_ptr_base.h line 1167

But it is not completly because if you look at the backtrace you will find that the second half is not making any sense.

Toolbox4_Firmware_V1_0_Vorfuehrung.ino (34,1 KB)

The hole code i use.
It has many header files.
Without pthread it stucks in the blinkLED and freezes.
So i need somehow break the while loop when the conditions are changed.

And, you didn't provide said files. So nobody can compile and test this.

The provided code includes no mention of pthreads.

Given the above, it's unclear what you expect anyone on the forum could possibly do to help you.

I tried to zip it and upload it but it is more than 8MB, so how can i upload it?

header_files.zip (12,9 KB)
The header files i wrote and use in the sketch

I doubt anyone is interested in slogging through that much code. Remember, people on this forum provide help for free. Perhaps you can pay someone on the Jobs and Paid Consultancy forum to fix your code for you.

Alternately, post an MRE. That's the smallest, simplest, complete code that compiles and demonstrates the problem at hand ... and only that problem.

The solution:
Before creating the pthread, the stack size should be tuned:

    int returnvalue;
    pthread_t tid;

    esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
    cfg.stack_size = (4 * 1024);
    esp_pthread_set_cfg(&cfg);

    returnvalue = pthread_create(&tid, NULL, blinkLED, (void *)&indx);

Because the error i encountered was stack over flow in the pthread function.
I wanted to post the solution may be some one find it helpful.

On ESP32, pthreads are just wrappers for FreeRTOS tasks. So, IMO, problems like this might be diagnosed and fixed more easily by foregoing pthreads and just using tasks directly.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.