8000
Skip to content

[wifi] Fix ESP8266 disconnect callback order to set error flag before notifying listeners - #13189

Merged
bdraco merged 2 commits into
devfrom
fix_callback_order_wifi_8266
Jan 13, 2026
Merged

[wifi] Fix ESP8266 disconnect callback order to set error flag before notifying listeners#13189
bdraco merged 2 commits into
devfrom
fix_callback_order_wifi_8266

Conversation

@bdraco
@bdraco bdraco commented Jan 13, 2026
Copy link
Copy Markdown
Member

What does this implement/fix?

Fix WiFi disconnect callback order on ESP8266 to set error_from_callback_ before notifying listeners, matching ESP-IDF and LibreTiny behavior.

Previously, error_from_callback_ was set AFTER the switch statement, which meant listeners were called before the flag was set. If a listener checked is_connected() during the disconnect callback, it would incorrectly return true because error_from_callback_ was still false.

This caused issues with WiFi roaming where the device would think it was still connected after a disconnect event, preventing proper reconnection logic from triggering.

Before (incorrect):

case EVENT_STAMODE_DISCONNECTED:
  // ... logging ...
  for (auto *listener : connect_state_listeners_) {
    listener->on_wifi_connect_state(...);  // Listeners called FIRST
  }
  break;
}

if (event->event == EVENT_STAMODE_DISCONNECTED) {
  error_from_callback_ = true;  // Flag set AFTER listeners!
}

After (correct, matches ESP-IDF):

case EVENT_STAMODE_DISCONNECTED:
  // ... logging ...
  error_from_callback_ = true;  // Flag set FIRST
  for (auto *listener : connect_state_listeners_) {
    listener->on_wifi_connect_state(...);  // Listeners called AFTER
  }
  break;
}

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Developer breaking change (an API change that could break external components)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

  • Reported in Discord by szupi_ipuzs - WiFi roaming not reconnecting properly on ESP8266

Pull request in esphome-docs with documentation (if applicable):

  • N/A (internal fix, no documentation changes needed)

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx
  • nRF52840

Example entry for config.yaml:

# No configuration changes needed - this is an internal fix
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

Additional Context

Bug Timeline

Date Commit Event Impact
April 17, 2019 6682c43d Original ESP8266 WiFi code created error_from_callback_ set after switch - latent bug (no callbacks yet)
July 27, 2020 08c8fa2c CVE-2020-12638 mitigation added Added error_from_callback_ for authmode downgrade, set correctly inside case
November 24, 2025 2bc8a4a7 Callbacks added to disconnect handler (PR #10748) Bug became active - callbacks called before flag is set
November 28, 2025 26e979d3 Converted to listener interfaces (PR #12155) Bug continued with new listener pattern

The bug was latent for ~6 years but only became an actual problem in November 2025 when disconnect callbacks were added inside the switch case while error_from_callback_ = true remained after the switch.

@github-actions
Copy link
Copy Markdown
Contributor

To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file:

external_components:
  - source: github://pr#13189
    components: [wifi]
    refresh: 1h

(Added by the PR bot)

@codecov-commenter
codecov-commenter commented Jan 13, 2026
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.76%. Comparing base (3d40979) to head (39f77a3).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #13189      +/-   ##
==========================================
+ Coverage   73.73%   73.76%   +0.02%     
==========================================
  Files          53       53              
  Lines       11329    11329              
  Branches     1538     1538              
==========================================
+ Hits         8354     8357       +3     
+ Misses       2574     2572       -2     
+ Partials      401      400       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link
Copy Markdown
Contributor

Memory Impact Analysis

Components: wifi
Platform: esp8266-ard

Metric Target Branch This PR Change
RAM 28,488 bytes 28,488 bytes ➡️ +0 bytes (0.00%)
Flash 323,083 bytes 323,083 bytes ➡️ +0 bytes (0.00%)
📊 Component Memory Breakdown
Component Target Flash PR Flash Change
[esphome]wifi 16,003 bytes 16,011 bytes 📈 🔸 +8 bytes (+0.05%)
🔍 Symbol-Level Changes (click to expand)

Changed Symbols

Symbol Target Size PR Size Change
esphome::wifi::WiFiComponent::wifi_event_callback(_esp_event*) 684 bytes 692 bytes 📈 +8 bytes (+1.17%)

Note: This analysis measures static RAM and Flash usage only (compile-time allocation).
Dynamic memory (heap) cannot be measured automatically.
⚠️ You must test this PR on a real device to measure free heap and ensure no runtime memory issues.

This analysis runs automatically when components change. Memory usage is measured from a representative test configuration.

@bdraco bdraco added this to the 2026.1.0b1 milestone Jan 13, 2026
@bdraco
bdraco marked this pull request as ready for review January 13, 2026 19:51
Copilot AI review requested due to automatic review settings January 13, 2026 19:51
@bdraco
bdraco commented Jan 13, 2026
Copy link
Copy Markdown
Member Author

thanks

@bdraco
bdraco merged commit a060d1d into dev Jan 13, 2026
38 of 39 checks passed
@bdraco
bdraco deleted the fix_callback_order_wifi_8266 branch January 13, 2026 21:33
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 15, 2026
@bdraco
bdraco removed the request for review from Copilot March 23, 2026 21:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0