change gpio0Updater task priority to 2 (higher than loop task) to prevent race condition when checking socket connected() and available()

This commit is contained in:
hathach 2025-07-25 23:38:22 +07:00
parent 54ee662201
commit 28ce73b8a6
No known key found for this signature in database
GPG key ID: 26FAB84F615C3C52
2 changed files with 16 additions and 15 deletions

View file

@ -2426,9 +2426,7 @@ CommandHandlerClass::CommandHandlerClass()
#if defined(CONFIG_IDF_TARGET_ESP32)
static const int GPIO_IRQ = 0;
#endif
#if defined(CONFIG_IDF_TARGET_ESP32C6)
#elif defined(CONFIG_IDF_TARGET_ESP32C6)
static const int GPIO_IRQ = 9;
#endif
@ -2442,7 +2440,8 @@ void CommandHandlerClass::begin()
_updateGpio0PinSemaphore = xSemaphoreCreateCounting(2, 0);
xTaskCreatePinnedToCore(CommandHandlerClass::gpio0Updater, "gpio0Updater", 8192, NULL, 1, NULL,
// change priority to higher than loop task() to prevent socket connected() and available() race condition
xTaskCreatePinnedToCore(CommandHandlerClass::gpio0Updater, "gpio0Updater", 8192, NULL, 2, NULL,
CONFIG_FREERTOS_NUMBER_OF_CORES-1);
}
@ -2489,26 +2488,26 @@ void CommandHandlerClass::updateGpio0Pin()
{
xSemaphoreTake(_updateGpio0PinSemaphore, portMAX_DELAY);
bool available = false;
int available = 0;
for (int i = 0; i < MAX_SOCKETS; i++) {
if (socketTypes[i] == TCP_MODE) {
if (tcpServers[i] && (tcpServers[i].hasClient() || tcpServers[i].accept())) {
available = true;
available = 1;
break;
} else if (tcpClients[i] && tcpClients[i].connected() && tcpClients[i].available()) {
available = true;
available = 1;
break;
}
}
if (socketTypes[i] == UDP_MODE && (udps[i].available() || udps[i].parsePacket())) {
available = true;
available = 1;
break;
}
if (socketTypes[i] == TLS_MODE && tlsClients[i] && tlsClients[i].connected() && tlsClients[i].available()) {
available = true;
if (socketTypes[i] == TLS_MODE && tlsClients[i].connected() && tlsClients[i].available()) {
available = 1;
break;
}
@ -2531,7 +2530,9 @@ void CommandHandlerClass::onWiFiReceive()
void CommandHandlerClass::handleWiFiReceive()
{
if (xPortInIsrContext()) {
xSemaphoreGiveFromISR(_updateGpio0PinSemaphore, NULL);
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR(_updateGpio0PinSemaphore, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
} else {
xSemaphoreGive(_updateGpio0PinSemaphore);
}

View file

@ -21,9 +21,9 @@ CONFIG_LOG_DEFAULT_LEVEL_WARN=y
CONFIG_LWIP_IRAM_OPTIMIZATION=y
CONFIG_LWIP_MAX_SOCKETS=6
CONFIG_LWIP_TCP_SYNMAXRTX=6
CONFIG_LWIP_TCP_MSS=1436
CONFIG_LWIP_TCP_SND_BUF_DEFAULT=5744
CONFIG_LWIP_TCP_WND_DEFAULT=5744
CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=2560
# CONFIG_LWIP_TCP_MSS=1436
# CONFIG_LWIP_TCP_SND_BUF_DEFAULT=5744
# CONFIG_LWIP_TCP_WND_DEFAULT=5744
# CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=4096
CONFIG_MBEDTLS_PSK_MODES=y
CONFIG_MBEDTLS_KEY_EXCHANGE_PSK=y