req->aux == null

Baldhead
Posts: 467
Joined: Sun Mar 31, 2019 5:16 am

req->aux == null

Postby Baldhead » Wed Aug 02, 2023 11:48 pm

Hi,

I am having some issues with my websocket secure server running in ESP32-S3.

When i sent a websocket message to client synchronously, the req->aux sometimes is null and sometimes in not null with the same client and same "session", ie, without the client reconnect again.

In httpd_ws.c file:

Code: Select all

esp_err_t httpd_ws_send_frame(httpd_req_t *req, httpd_ws_frame_t *frame)
{
    ESP_LOGI(TAG, "[%s] req: %p\n", __func__, req );
    ESP_LOGI(TAG, "[%s] req->aux: %p\n", __func__, req->aux );
    
    esp_err_t ret = httpd_ws_check_req(req);
    if (ret != ESP_OK) {
        return ret;
    }
    return httpd_ws_send_frame_async(req->handle, httpd_req_to_sockfd(req), frame);
}

Code: Select all

I (110924) httpd_ws: [httpd_ws_send_frame] req: 0x3fcc2c7c
I (110927) httpd_ws: [httpd_ws_send_frame] req->aux: 0x0

W (110934) httpd_ws: httpd_ws_check_req: Argument is null
What can be the causes for req->aux to be null ?

Stack corruption ?
Do i need to increase the httpd task stack ?

Edit:
i increased the httpd task stack from 10240 to 20480 and didn't solve the problem.
i increased the lwip task stack from 3072 to 4096(with httpd task stack 20480) and didn't solve the problem too.

@ESP_cermak
Last edited by Baldhead on Fri Aug 04, 2023 1:04 am, edited 1 time in total.

Baldhead
Posts: 467
Joined: Sun Mar 31, 2019 5:16 am

Re: req->aux == null

Postby Baldhead » Fri Aug 04, 2023 1:01 am

I changed compiler optimization performance level -O2 to debug -O0 and at runtime i get:
"A stack overflow in task sys_evt has been detected".

I removed 2 local buffers with 128 bytes each(consumed 256 bytes task stack) from a ota callback function "ota_event_handler" and the stack overflow still occurs.

Code: Select all

ESP_ERROR_CHECK(esp_event_handler_register(ESP_HTTPS_OTA_EVENT, ESP_EVENT_ANY_ID, &ota_event_handler, NULL));
then i increased the stack from 2304 to 3072 bytes in the task sys_evt and the stack overflow disappeared(worked).

I thought i found the problem of req->aux = null, but doesn't, the problem still persists.

Baldhead
Posts: 467
Joined: Sun Mar 31, 2019 5:16 am

Re: req->aux == null

Postby Baldhead » Fri Aug 04, 2023 1:54 am

Maybe i cant send a message from "Event loop task" to http task ?

wss_server.c file:

Code: Select all

void resp_Ws_With_Message_Sync( httpd_req_t *req, char* message, uint8_t command )
{
    ESP_LOGI(TAG, LOG_USER("FUNCTION: %s"), __func__ ); 
    ESP_LOGI(TAG, LOG_USER("Message: %s\n"), message);

    ESP_LOGI(TAG, LOG_USER("[%s] req: %p"), __func__, req );
    ESP_LOGI(TAG, LOG_USER("[%s] req->aux: %p\n"), __func__, req->aux );


    size_t strSize = strlen(message);
    init_ws_pkt( 1 + strSize );  // command size + error message size 
    ws_pkt.payload[0] = command;       
    
    for( uint32_t i = 0 ; i < strSize ; i++ )
    {
        ws_pkt.payload[i+1] = message[i];  // add a message after the command.
    }

    httpd_ws_send_frame( req, &ws_pkt );
}

my_ota.c file:

Code: Select all

static httpd_req_t* http_req = NULL;

////////////////////////////////////////////////////////////////////////////////////////////
// Send eventhandler messages to phone app and lcd display.

static void send_ota_message( char* message )
{
    ESP_LOGI(TAG, LOG_USER("FUNCTION: %s"), __func__ ); 
    ESP_LOGI(TAG, LOG_USER("[%s] http_req: %p\n"), __func__, http_req );


    if( http_req != NULL )
    { 
        resp_Ws_With_Message_Sync( http_req, message, req_command );

        // send message to lcd display
    }
    else
    {
        // send message to lcd display
    }
}

////////////////////////////////////////////////////////////////////////////////////////////
// Event handler for catching ota system events

static void ota_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
    ESP_LOGI(TAG, LOG_USER("FUNCTION: %s"), __func__ ); 
    
    if( event_base == ESP_HTTPS_OTA_EVENT )
    {
        switch( event_id )
        {
            case ESP_HTTPS_OTA_START:
            {                
                ESP_LOGI(TAG, TEXT_OTA_START);
                send_ota_message( TEXT_OTA_START );  // calback need to return quickly, try create a task to send the message to websocket client. Send message from this task to new task using queue.               
                break;
            }

            case ESP_HTTPS_OTA_CONNECTED:
            {                
                ESP_LOGI(TAG, TEXT_OTA_CONNECTED);
                send_ota_message( TEXT_OTA_CONNECTED ); 
                break;
            }
            
            case ESP_HTTPS_OTA_GET_IMG_DESC:
            {               
                ESP_LOGI(TAG, TEXT_OTA_GET_IMG_DESC);
                send_ota_message( TEXT_OTA_GET_IMG_DESC ); 
                break;
            }

            case ESP_HTTPS_OTA_VERIFY_CHIP_ID:
            {               
                ESP_LOGI(TAG, TEXT_OTA_VERIFY_CHIP_ID"%d", *(esp_chip_id_t *)event_data);
                                
                char* str = NULL; 
                if( concat_text_int( TEXT_OTA_VERIFY_CHIP_ID, *(esp_chip_id_t *)event_data, &str ) != ESP_OK ) return;
                send_ota_message( str );                
                free(str);

                break;
            }

            case ESP_HTTPS_OTA_DECRYPT_CB:
            {          
                ESP_LOGI(TAG, TEXT_OTA_DECRYPT_CB);
                send_ota_message( TEXT_OTA_DECRYPT_CB );            
                break;
            }

            case ESP_HTTPS_OTA_WRITE_FLASH:
            {            
                ESP_LOGI(TAG, LOG_USER1(TEXT_OTA_WRITE_FLASH"%d"), *(int *)event_data);
                                
                char* str = NULL; 
                if( concat_text_int( TEXT_OTA_WRITE_FLASH, *(int *)event_data, &str ) != ESP_OK ) return;
                send_ota_message( str );                
                free(str);        

                break;
            }

            case ESP_HTTPS_OTA_UPDATE_BOOT_PARTITION:
            {                               
                ESP_LOGI(TAG, TEXT_OTA_UPDATE_BOOT_PARTITION"%d", *(esp_partition_subtype_t *)event_data);  
                
                char* str = NULL; 
                if( concat_text_int( TEXT_OTA_UPDATE_BOOT_PARTITION, *(esp_partition_subtype_t *)event_data, &str ) != ESP_OK ) return;
                send_ota_message( str );                
                free(str); 

                break;
            }

            case ESP_HTTPS_OTA_FINISH:
            {                
                ESP_LOGI(TAG, TEXT_OTA_FINISH);
                send_ota_message( TEXT_OTA_FINISH );                                                
                http_req = NULL;                
                break;
            }

            case ESP_HTTPS_OTA_ABORT:
            {
                ESP_LOGI(TAG, TEXT_OTA_ABORT);
                send_ota_message( TEXT_OTA_ABORT );                
                http_req = NULL;                
                break;
            }
        }
    }
}

Baldhead
Posts: 467
Joined: Sun Mar 31, 2019 5:16 am

Re: req->aux == null

Postby Baldhead » Tue Aug 08, 2023 9:02 pm

I created another task, that receive a struct by copy from a queue, from ota_handler, .

I implemented it like this now:
The ota_handler send a struct by copy(through a queue) to another task(send_message_task).
In the handler, the queue send a struct data and return immediately, since the handler needs to return quickly to continue executing the event task.
The "send_message_task" receive the struct, through the queue, and send the message using my "websocket server".

The req is not null.
The req->aux is null.

How can req not be null and req->aux be null ?

I still haven't found the problem.

Some suggestion ?

Baldhead
Posts: 467
Joined: Sun Mar 31, 2019 5:16 am

Re: req->aux == null

Postby Baldhead » Thu Aug 10, 2023 10:49 pm

I disabled https, only using http, same problem.

The wifi and lwip and mbedtls memory were in the spiram, i put everything in the internal memory and still the same problem persists.

Baldhead
Posts: 467
Joined: Sun Mar 31, 2019 5:16 am

Re: req->aux == null

Postby Baldhead » Fri Aug 11, 2023 11:43 pm

With the suggestion of @ESP-YJM i found the problem.

The req->aux is set to null every time the uri handler returns.
This is not persistent until the "close socket", it is only valid per "client message reception".

This way i can't use "httpd_ws_send_frame( req, &ws_pkt );" that is "sync".

I have to use "httpd_ws_send_frame_async(server, sockfd, &ws_pkt);", that is async .

Now i understand why there is send message sync and send message async in espressif websocket server.

Thank's again @ESP-YJM.

Who is online

Users browsing this forum: No registered users and 73 guests