diff --git a/components/esp_http_server/src/esp_httpd_priv.h b/components/esp_http_server/src/esp_httpd_priv.h index 54c6923c6d..ca57ceea14 100644 --- a/components/esp_http_server/src/esp_httpd_priv.h +++ b/components/esp_http_server/src/esp_httpd_priv.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2018-2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2018-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -576,6 +576,16 @@ esp_err_t httpd_ws_get_frame_type(httpd_req_t *req); */ esp_err_t httpd_sess_trigger_close_(httpd_handle_t handle, struct sock_db *session); +/** + * @brief Directly closes the least recently used session + * + * @param[in] hd Server instance data + * + * @return + * - ESP_OK : if session closed successfully + */ +esp_err_t httpd_sess_close_lru_direct(struct httpd_data *hd); + /** End of WebSocket related functions * @} */ diff --git a/components/esp_http_server/src/httpd_main.c b/components/esp_http_server/src/httpd_main.c index 943075317f..61b44d086d 100644 --- a/components/esp_http_server/src/httpd_main.c +++ b/components/esp_http_server/src/httpd_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2018-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2018-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -54,7 +54,12 @@ static esp_err_t httpd_accept_conn(struct httpd_data *hd, int listen_fd) if (hd->config.lru_purge_enable == true) { if (!httpd_is_sess_available(hd)) { /* Queue asynchronous closure of the least recently used session */ +#if CONFIG_HTTPD_QUEUE_WORK_BLOCKING + /* In case of blocking mode, close the least recently used session directly */ + return httpd_sess_close_lru_direct(hd); +#else return httpd_sess_close_lru(hd); +#endif /* CONFIG_HTTPD_QUEUE_WORK_BLOCKING */ /* Returning from this allows the main server thread to process * the queued asynchronous control message for closing LRU session. * Since connection request hasn't been addressed yet using accept() diff --git a/components/esp_http_server/src/httpd_sess.c b/components/esp_http_server/src/httpd_sess.c index eaeaee7c1a..5b2fd3e347 100644 --- a/components/esp_http_server/src/httpd_sess.c +++ b/components/esp_http_server/src/httpd_sess.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2018-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -498,3 +498,21 @@ void httpd_sess_close_all(struct httpd_data *hd) }; httpd_sess_enum(hd, enum_function, &context); } + +esp_err_t httpd_sess_close_lru_direct(struct httpd_data *hd) +{ + enum_context_t context = { + .task = HTTPD_TASK_FIND_LOWEST_LRU, + .lru_counter = UINT64_MAX, + .fd = -1 + }; + httpd_sess_enum(hd, enum_function, &context); + if (!context.session) { + return ESP_OK; + } + + ESP_LOGD(TAG, LOG_FMT("Directly closing session with fd %d"), context.session->fd); + // Call httpd_sess_delete directly instead of going through work queue + httpd_sess_delete(hd, context.session); + return ESP_OK; +}