8000
Skip to content

[esp32_ble_tracker] Fix false reboots when event loop is blocked - #10144

Merged
kbx81 merged 1 commit into
devfrom
scan_loop_blocked_fix
Aug 10, 2025
Merged

[esp32_ble_tracker] Fix false reboots when event loop is blocked#10144
kbx81 merged 1 commit into
devfrom
scan_loop_blocked_fix

Conversation

@bdraco
@bdraco bdraco commented Aug 8, 2025
Copy link
Copy Markdown
Member

What does this implement/fix?

This PR fixes a race condition in the ESP32 BLE tracker where the device would incorrectly reboot when the event loop was blocked.

Previously, the scan timeout was monitored using the scheduler's set_timeout() mechanism. When the event loop was blocked (e.g., due to heavy processing or network delays), the scheduler would fire immediately upon unblocking, before the ESP32 BLE tracker's loop() method could process any pending scan completion events. This caused false positive timeouts and unnecessary reboots.

The fix moves the timeout monitoring from the scheduler into the ESP32 BLE tracker's loop() method using a state machine. When a timeout is detected, it now waits one additional loop iteration before rebooting, ensuring all components (particularly esp32_ble) have had a chance to process pending events.

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)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

  • fixes #(issue number if applicable)

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

  • N/A (internal implementation change, no user-facing changes)

Test Environment

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

Example entry for config.yaml:

# No configuration changes required - this is an internal bugfix
esp32_ble_tracker:
  scan_parameters:
    duration: 5min  # Timeout is 2x this value (10 minutes)

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:

Copilot AI review requested due to automatic review settings August 8, 2025 21:00
@github-actions
github-actions Bot commented Aug 8, 2025
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#10144
    components: [esp32_ble_tracker]
    refresh: 1h

(Added by the PR bot)

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a race condition in the ESP32 BLE tracker that was causing false reboots when the event loop was blocked. The issue occurred because the scan timeout was being monitored through the scheduler, which would fire immediately when the loop unblocked, before the BLE tracker could process pending scan completion events.

Key changes:

  • Replaced scheduler-based timeout monitoring with a state machine approach in the loop() method
  • Added a one-loop delay before rebooting to ensure all components can process pending events
  • Moved timeout logic from external scheduler callbacks into the component's main loop

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
esp32_ble_tracker.h Added state machine enum and variables for tracking scan timeout state
esp32_ble_tracker.cpp Implemented new timeout monitoring logic in loop() method and updated scan start/stop methods

Comment thread esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp
Comment thread esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp
@codecov-commenter
codecov-commenter commented Aug 8, 2025
Copy link
Copy Markdown
8000

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 60.21%. Comparing base (676c51f) to head (ea121cc).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev   #10144   +/-   ##
=======================================
  Coverage   60.21%   60.21%           
=======================================
  Files          51       51           
  Lines       10375    10375           
  Branches     1381     1381           
=======================================
  Hits         6247     6247           
  Misses       3772     3772           
  Partials      356      356           

☔ 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.

@bdraco bdraco added this to the 2025.8.0b1 milestone Aug 8, 2025
@kbx81 kbx81 left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 👍🏻

@kbx81
kbx81 merged commit cef39e7 into dev Aug 10, 2025
78 checks passed
@kbx81
kbx81 deleted the scan_loop_blocked_fix branch August 10, 2025 09:44
@shvmm
shvmm commented Aug 10, 2025
Copy link
Copy Markdown
Contributor
image

Device uptime more than 24 hours. No reboots since OTA. Many Thanks ❤️

@bdraco
bdraco commented Aug 10, 2025
Copy link
Copy Markdown
Member Author

Thanks!

@github-actions github-actions Bot locked and limited conversation to collaborators Aug 12, 2025
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.

5 participants

0