Defer BLE-callback display teardown and MSD update to the main loop (ESP32)#74
Open
balloob wants to merge 1 commit into
Open
Defer BLE-callback display teardown and MSD update to the main loop (ESP32)#74balloob wants to merge 1 commit into
balloob wants to merge 1 commit into
Conversation
…ESP32) onConnect() and onDisconnect() run in the BLE host task. onConnect() called updatemsdata() (I2C sensor reads + shared-state mutation) and onDisconnect() called cleanupDirectWriteState() (bbepSleep/SPI.end/pwrmgm) directly. Both race loop(): a disconnect firing while loop() is mid bbepWriteData powers the panel down under an in-flight SPI transfer and clears state the handler is still using; onConnect's updatemsdata races the loop's I2C/statics. Set pending flags in the callbacks (like the existing bleRestartAdvertisingPending) and do the work in loop() instead. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MgW3EZCfMkGFffseDPwV74
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On ESP32,
onConnect()andonDisconnect()run in the BLE host task, but they performed work that races the Arduinoloop()task:onConnect()calledupdatemsdata()— I2C sensor reads and mutation of shared MSD/advertising state — concurrently with the same work inloop().onDisconnect()calledcleanupDirectWriteState(true)—bbepSleep/SPI.end()/Wire.end()/pwrmgm(false)— whileloop()may be midbbepWriteDataon the same SPI bus during a direct-write upload (theepdRefreshInProgressguard only covers the refresh phase, not the upload phase). That powers the panel down under an in-flight transfer and clears state the handler is still using.Fix
Set pending flags in the callbacks and perform the work in
loop(), mirroring the existingbleRestartAdvertisingPendingdeferral:onConnectsetsbleConnectMsdUpdatePending,onDisconnectsetsbleDirectWriteCleanupPending, andloop()acts on them right after the advertising-restart handling.Verification
esp32-N4andnrf52840custom.onDisconnectblock as #56 (touch-suspend), so the two will need a trivial merge.