Stack canary watchpoint triggered (BTC_TASK)

Hello,
I am trying to implement an OTA script. This is my ota code:

void initOTA(){
  Serial.println("initOTA");
  #ifdef USE_SPIFFS
  /*
    FORMAT_SPIFFS_IF_FAILED is a boolean with true value
    This means if begin function returns false,
    the SPIFFS file system will be formatted to attempt recovery.
  */
  Serial.println("geldi");
  
  SPIFFS.begin(true);
  Serial.println("gelmedi");
/*
  if (!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)) {
    Serial.println("Gelmedi");
    statusOTA = AllMemErr;
    return;
    }
    */
  #else
    if (!FFat.begin()) {
      statusOTA = AllMemErr;
      //if begin returns false, format the FFat file system
      if (FORMAT_FFAT_IF_FAILED) FFat.format();
      return;
    }    
  #endif
}

void runOTA(){
  switch (MODE){
    case UPDATE_MODE:
      if (writeFile) {
        if (!current) {
          writeBinaryData(FLASH, "/update.bin", updater, writeLen);
        } else {
          writeBinaryData(FLASH, "/update.bin", updater2, writeLen2);
        }
      }
      break;
    
    case OTA_MODE:
      if (rParts == tParts) {
          updateFromFileSystem(FLASH);
      } 
      break;
    default:
      break;
  }
}

This is my onWrite BLE function:

class MyCallbacks: public BLECharacteristicCallbacks
{
    void onWrite(BLECharacteristic *pCharacteristic)
    {
      //keep track of new incoming data
      newData = true;
      
      //getValue() returns a string and commonly used 
      //characteristics value represents textual or 
      //string data
      std::string value = pCharacteristic->getValue();
      
      //getData() returns an uint8_t pointer and in this 
      //case it points to an array of bytes. It is often 
      //used when the characteristic's value is a sequence 
      //of raw bytes, such as binary data.
      pData = pCharacteristic->getData();
      size_t otaLength = pCharacteristic->getValue().length();
      if (pData[0] == 0xA0){
        //first command for OTA received
        if(!enterOTA){
          size_t freeHeap = esp_get_free_heap_size();
          UBaseType_t stackHighWaterMark = uxTaskGetStackHighWaterMark(NULL);
          size_t freeHeap8Bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);

          initOTA();
          enterOTA = true;
        }
        if(enterOTA){
          //handle incoming binary data
          handleOTA(pData, otaLength);
          //continuosly check OTA state
          runOTA();
        }
      }
    }
};

Finally this is my ini file:

; PlatformIO Project Configuration File

;

; Build options: build flags, source filter

; Upload options: custom upload port, speed and extra flags

; Library options: dependencies, extra library storages

; Advanced options: extra scripting

;

; Please visit documentation for the other options and examples

; https://docs.platformio.org/page/projectconf.html

[env:esp32-s3-devkitc-1]

platform = espressif32

board = esp32-s3-devkitc-1

;https://github.com/platformio/platform-espressif32/blob/master/boards/esp32-s3-devkitc-1.json

; change MCU frequency

board_build.f_cpu = 240000000L

framework = arduino

build_flags = -I lib

lib_deps =

Adafruit BNO08x@^1.2.5

So the problem is, whenever I send A0, it enters the initOTA() function. And stops exactly at the SPIFFS.begin(true); row. I am getting
Guru Meditation Error: Core 0 panic’ed
Stack canary watchpoint triggered (BTC_TASK)
error. ESP reboot itself after that. What is the problem here? How can i solve it

By initializing the OTA filesystem inside a BLE callback, you seem to be exhausting the available stack of the Bluetooth task function. It only has a fixed amount of stack available to execute its functions, and the init OTA function seems to be wanting too much for it. Try and refactor it so that the OTA init doesn’t happen in the bluetooth task, but maybe in the main task which has more stack free at that point, synchronized by some FreeRTOS functionality (e.g., task notifications, queues, etc.)

See

For example, the easiest way should be to just initalize the SPIFFS directly in setup(), not in the bluetooth task?