fix: improve BLE connection stability and fix memory leaks in noble-ble-handler#654
fix: improve BLE connection stability and fix memory leaks in noble-ble-handler#654
Conversation
Revert the import order changes in noble-ble-handler.ts that were accidentally included in a previous commit.
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
Pull request was converted to draft
14330a5 to
8ba5e24
Compare
|
No dependency changes detected. Learn more about Socket for GitHub. 👍 No dependency changes detected in pull request |
fe93846 to
14330a5
Compare
- Add timeout and disconnect protection to discoverServicesAndCharacteristics - Fix disconnect listener lost after forceReconnectPeripheral by re-setting it - Remove redundant forceReconnect in freshScanAndDiscover (fresh peripheral doesn't need GATT cache clearing) - Add cleanupDiscoveredCache option to DeviceCleanupOptions - Pass webContents to forceReconnectPeripheral and freshScanAndDiscover for proper listener setup - Convert all Chinese comments to English Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
@codex review, Check if there are any areas in the current code that can be optimized |
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 49a9042fc4
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
BLE peripherals may advertise with empty/changed localName after pairing or on some OSes. The previous code checked isOnekeyDevice(deviceName) before checking activeTargetedScan, which meant the targeted scan would never resolve for devices with missing/changed names. Move the activeTargetedScan check before the name filter so we match by device ID regardless of name during targeted scans.
- Remove global activeTargetedScan state variable - Remove settled flag - use cleanup + Promise idempotency instead - Use local discover listener within performTargetedScan - Cleanup function removes listener and clears timeout atomically - Simpler, more maintainable code with no global state Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Fix typo: "bundlinpissues" -> "bundling issues" - Fix enumerateDevices interval memory leak with unified cleanup function - Remove unused `idx` variable in chunked write loop - Extract duplicated connection logic into setupConnectionAndDiscoverServices() - Net reduction of 22 lines while improving maintainability Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Use const for timeoutId (prefer-const) - Fix prettier formatting for reject() call - Fix prettier formatting for function signature - Remove redundant await on return value (no-return-await) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add try/finally to subscribeNotifications to ensure subscriptionOperations is always reset even on error - Use ERRORS.TypedError consistently instead of plain Error in freshScanAndDiscover and discoverServicesAndCharacteristicsWithRetry - Wait for disconnect callback before rejecting in connectDevice - Remove obsolete comment Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Summary
This PR improves BLE connection stability and fixes several memory leaks in the
noble-ble-handler.tsmodule.Changes
activeTargetedScanstate to eliminate listener leaksactiveTargetedScanBEFORE theisOnekeyDevicename filter to handle BLE peripherals that advertise with empty/changedlocalNameafter pairing or on some OSesSERVICE_DISCOVERY_TIMEOUT(10s) and disconnect handler todiscoverServicesAndCharacteristicsto prevent hanging operationscleanupDiscoveredCacheoption: New option inDeviceCleanupOptionsto optionally clear the discovered devices cachewebContentsto reconnect functions: Enable proper disconnect listener setup inforceReconnectPeripheralandfreshScanAndDiscoverFiles Changed
packages/hd-transport-electron/src/noble-ble-handler.tsTest Plan