1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-01 17:42:22 +01:00

[wifi] Fix crash during WiFi reconnection on ESP32 with poor signal quality

This commit is contained in:
J. Nick Koston
2025-07-30 13:16:40 -10:00
parent f4f69e827b
commit 79bee386ff
2 changed files with 36 additions and 0 deletions

View File

@@ -473,6 +473,12 @@ bool WiFiComponent::wifi_sta_ip_config_(optional<ManualIP> manual_ip) {
if (!this->wifi_mode_(true, {}))
return false;
// Check if the STA interface is initialized before using it
if (s_sta_netif == nullptr) {
ESP_LOGW(TAG, "STA interface not initialized");
return false;
}
esp_netif_dhcp_status_t dhcp_status;
esp_err_t err = esp_netif_dhcpc_get_status(s_sta_netif, &dhcp_status);
if (err != ESP_OK) {
@@ -691,6 +697,8 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
} else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_STOP) {
ESP_LOGV(TAG, "STA stop");
s_sta_started = false;
// Clear the STA interface handle to prevent use-after-free
s_sta_netif = nullptr;
} else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_AUTHMODE_CHANGE) {
const auto &it = data->data.sta_authmode_change;
@@ -789,6 +797,10 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
} else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STOP) {
ESP_LOGV(TAG, "AP stop");
s_ap_started = false;
#ifdef USE_WIFI_AP
// Clear the AP interface handle to prevent use-after-free
s_ap_netif = nullptr;
#endif
} else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_PROBEREQRECVED) {
const auto &it = data->data.ap_probe_req_rx;
@@ -865,6 +877,12 @@ bool WiFiComponent::wifi_ap_ip_config_(optional<ManualIP> manual_ip) {
if (!this->wifi_mode_({}, true))
return false;
// Check if the AP interface is initialized before using it
if (s_ap_netif == nullptr) {
ESP_LOGW(TAG, "AP interface not initialized");
return false;
}
esp_netif_ip_info_t info;
if (manual_ip.has_value()) {
info.ip = manual_ip->static_ip;