Semaphore control within an extrenal class

Hi,

I have a project with multiple tasks and a “AsyncelegantOTA” webserver. The webserver is to be used for OTA as well as an interface to set up some base data transfer for the other tasks running. I’m struggling to implement a semaphore to ensure the data is not accessed at the same time. Any tips on how to implement this. As shown below, I have a handler that is passed during setup from Main to the Esp32_OTA class.

class Esp32_OTA {
public:
    
    SemaphoreHandle_t _sem_Ctrl_IOs = NULL;
}

void Esp32_OTA::InitOTA( SemaphoreHandle_t sem_Ctrl_IOs){
  _sem_Ctrl_IOs = sem_Ctrl_IOs;
}

String Esp32_OTA::outputState(int output){
  if(_sem_Ctrl_IOs != NULL){
  if (xSemaphoreTake(_sem_Ctrl_IOs, 1) == pdPASS ) {
    if(digitalRead(output)){
      return "checked";
    }
    else {
      return "";
    }           
    xSemaphoreGive(_sem_Ctrl_IOs);  
  }
  }

The issue, once the semaphore is taken within the outputState() function, the system aborts and restart. Any tips on where I’m going wrong?

The xSemaphoreGive(_sem_Ctrl_IOs); is never reached, causing the semaphore to be locked forever. You need to give the semaphore back before returning.