Fix BLE disconnect crash and event listener leaks#475
Open
makermelissa-piclaw wants to merge 1 commit intocircuitpython:betafrom
Open
Fix BLE disconnect crash and event listener leaks#475makermelissa-piclaw wants to merge 1 commit intocircuitpython:betafrom
makermelissa-piclaw wants to merge 1 commit intocircuitpython:betafrom
Conversation
- Guard connectionStep() in updateConnected() behind connectDialog.isOpen() to prevent 'Modal has not been opened yet' crash when BLE disconnects during file write operations (fixes circuitpython#377) - Store bound event handlers in constructor and reuse them so removeEventListener actually removes the old listener - Fix advertisement listener cleanup in connectToBluetoothDevice()
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
Fixes #377 — BLE disconnect during file write operations throws
Modal has not been opened yet. No instance availableand, in most cases, prevents the UI from being used again without a page refresh.Root cause
Two separate bugs in
js/workflows/ble.jscompound into the reported crash:Bug 1 —
connectionStep()called when no dialog is openWhen BLE disconnects mid-file-write (not during the connect flow), the chain is:
getModal()throws becauseconnectDialogwas never opened in this lifecycle — the user was editing a file, not walking the connect wizard. This matches the stack trace in the issue exactly.Bug 2 — Event listener removal no-ops
switchToDevice()andconnectToSerial()attempt to clean up listeners with:Every
.bind(this)call returns a new function reference, soremoveEventListenernever matches the previously-added listener. Each reconnect leaks another handler, causing duplicate disconnect callbacks (which makes Bug 1 fire multiple times on a single disconnect).The same pattern exists for
onSerialReceiveand the advertisement listener inconnectToBluetoothDevice().Fix
connectionStep()inupdateConnected()behindconnectDialog.isOpen()so step-marking only runs when the dialog actually exists._boundOnDisconnected,_boundOnSerialReceive) and reuse the same reference for bothaddEventListenerandremoveEventListener.connectToBluetoothDevice()the same way (_boundOnAdvertisementReceived), and remove any previous listener before attaching a new one.Testing
npx vite buildpasses cleanly.Reproduced the crash signature against the unpatched
getModal()path and verified the guard short-circuits it while preserving normal behavior when the dialog is open:Modal has not been opened yetHardware end-to-end (file write → disconnect) testing was limited by Linux Chromium
watchAdvertisementsflakiness; local-only bench review confirms the listener-identity fix, and reviewers on macOS/Windows Chrome can exercise the full flow.Notes
Only
js/workflows/ble.jsis modified. No behavior change for connected users or normal flows — only the error path is fixed.