Skip to content

fix: improve BLE connection stability and fix memory leaks in noble-ble-handler#654

Draft
wabicai wants to merge 12 commits intoonekeyfrom
fix/revert-noble-import-changes
Draft

fix: improve BLE connection stability and fix memory leaks in noble-ble-handler#654
wabicai wants to merge 12 commits intoonekeyfrom
fix/revert-noble-import-changes

Conversation

@wabicai
Copy link
Member

@wabicai wabicai commented Jan 20, 2026

Summary

This PR improves BLE connection stability and fixes several memory leaks in the noble-ble-handler.ts module.

Changes

  • Fix memory leak in targeted scan: Replace per-call listener pattern with global activeTargetedScan state to eliminate listener leaks
  • Fix targeted scan bypassing name filter: Check activeTargetedScan BEFORE the isOnekeyDevice name filter to handle BLE peripherals that advertise with empty/changed localName after pairing or on some OSes
  • Add timeout and disconnect protection: Add SERVICE_DISCOVERY_TIMEOUT (10s) and disconnect handler to discoverServicesAndCharacteristics to prevent hanging operations
  • Fix disconnect listener lost after forceReconnectPeripheral: Re-setup disconnect listener after reconnect to ensure device disconnect events are properly handled
  • Add cleanupDiscoveredCache option: New option in DeviceCleanupOptions to optionally clear the discovered devices cache
  • Pass webContents to reconnect functions: Enable proper disconnect listener setup in forceReconnectPeripheral and freshScanAndDiscover
  • Remove redundant forceReconnect in freshScanAndDiscover: Fresh peripheral doesn't need GATT cache clearing
  • Code cleanup: Convert all Chinese comments to English

Files Changed

  • packages/hd-transport-electron/src/noble-ble-handler.ts

Test Plan

  • Test BLE device connection on macOS
  • Test BLE device connection on Windows
  • Verify device disconnect events are properly received
  • Test reconnection scenarios after device disconnects
  • Test targeted scan for devices with empty/missing localName
  • Monitor for memory leaks during extended usage

Revert the import order changes in noble-ble-handler.ts that were
accidentally included in a previous commit.
@wabicai wabicai enabled auto-merge (squash) January 20, 2026 02:50
@revan-zhang
Copy link
Contributor

revan-zhang commented Jan 20, 2026

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@wabicai wabicai changed the title fix: revert noble-ble-handler import order changes fix: noble-ble-handler memory leak Jan 20, 2026
@wabicai wabicai marked this pull request as draft January 20, 2026 02:52
auto-merge was automatically disabled January 20, 2026 02:52

Pull request was converted to draft

@wabicai wabicai force-pushed the fix/revert-noble-import-changes branch from 14330a5 to 8ba5e24 Compare January 26, 2026 03:03
@wabicai wabicai marked this pull request as ready for review January 26, 2026 03:03
@socket-security
Copy link

socket-security bot commented Jan 26, 2026

No dependency changes detected. Learn more about Socket for GitHub.

👍 No dependency changes detected in pull request

@wabicai wabicai marked this pull request as draft January 26, 2026 03:08
@wabicai wabicai force-pushed the fix/revert-noble-import-changes branch 2 times, most recently from fe93846 to 14330a5 Compare January 26, 2026 11:09
wabicai and others added 2 commits January 26, 2026 19:12
- 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>
@wabicai wabicai marked this pull request as ready for review January 26, 2026 16:40
@wabicai wabicai changed the title fix: noble-ble-handler memory leak fix: improve BLE connection stability and fix memory leaks in noble-ble-handler Jan 26, 2026
@wabicai
Copy link
Member Author

wabicai commented Jan 26, 2026

@codex review, Check if there are any areas in the current code that can be optimized

@wabicai
Copy link
Member Author

wabicai commented Jan 26, 2026

Code review

No 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 👎.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 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.
@wabicai wabicai marked this pull request as draft January 26, 2026 17:00
wabicai and others added 6 commits January 27, 2026 11:02
- 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments