Skip to content

Commit

Permalink
Merge branch 'test/modbus_lwip_issue' into 'master'
Browse files Browse the repository at this point in the history
modbus: Exit server task gracefully to correctly cleanup lwip internals

Closes IDFGH-4432

See merge request espressif/esp-idf!12075
  • Loading branch information
david-cermak committed Feb 11, 2021
2 parents a196d6d + 898cac0 commit 61f3af0
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion components/freemodbus/tcp_slave/port/port_tcp_slave.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void vMBPortEventClose( void );

/* ----------------------- Static variables ---------------------------------*/
static int xListenSock = -1;
static SemaphoreHandle_t xShutdownSemaphore = NULL;
static MbSlavePortConfig_t xConfig = { 0 };

/* ----------------------- Static functions ---------------------------------*/
Expand Down Expand Up @@ -462,6 +463,11 @@ static void vMBTCPPortServerTask(void *pvParameters)
// Wait for an activity on one of the sockets, timeout is NULL, so wait indefinitely
xErr = select(xMaxSd + 1 , &xReadSet , NULL , NULL , NULL);
if ((xErr < 0) && (errno != EINTR)) {
// First check if the task is not flagged for shutdown
if (xListenSock == -1 && xShutdownSemaphore) {
xSemaphoreGive(xShutdownSemaphore);
vTaskDelete(NULL);
}
// error occurred during wait for read
ESP_LOGE(MB_TCP_SLAVE_PORT_TAG, "select() errno = %d.", errno);
continue;
Expand Down Expand Up @@ -626,8 +632,22 @@ void
vMBTCPPortClose( )
{
// Release resources for the event queue.

// Try to exit the task gracefully, so select could release its internal callbacks
// that were allocated on the stack of the task we're going to delete
xShutdownSemaphore = xSemaphoreCreateBinary();
vTaskResume(xConfig.xMbTcpTaskHandle);
if (xShutdownSemaphore == NULL || // if no semaphore (alloc issues) or couldn't acquire it, just delete the task
xSemaphoreTake(xShutdownSemaphore, 2*pdMS_TO_TICKS(CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND)) != pdTRUE) {
ESP_LOGE(MB_TCP_SLAVE_PORT_TAG, "Task couldn't exit gracefully within timeout -> abruptly deleting the task");
vTaskDelete(xConfig.xMbTcpTaskHandle);
}
if (xShutdownSemaphore) {
vSemaphoreDelete(xShutdownSemaphore);
xShutdownSemaphore = NULL;
}

vMBPortEventClose( );
vTaskDelete(xConfig.xMbTcpTaskHandle);
}

void
Expand Down

0 comments on commit 61f3af0

Please sign in to comment.