Skip to content

Close BLE transports after disconnects and timeouts - #1

Open
kzkvv wants to merge 2 commits into
mainfrom
codex/fix-ble-disconnect-cleanup
Open

kzkvv wants to merge 2 commits into
mainfrom
codex/fix-ble-disconnect-cleanup

Conversation

@kzkvv

@kzkvv kzkvv commented Sep 17, 2026 •

Copy link
Copy Markdown
Owner

BLE disconnects left retired Bleak clients holding D-Bus sockets. On a Home Assistant host, 233 connections belonged to HA when the system bus exhausted its 256-per-user limit and STREAM Ultra telemetry stopped.

Retain each client until bounded cleanup finishes, even when is_connected is false. Keep cleanup outside cancellable auth/unload tasks, shield waiters, wait before reconnecting, and ignore old-client callbacks.

If BlueZ disconnect times out or raises, release that client's remaining D-Bus socket, device watcher, and disconnect monitor. Bleak 3.0.2 has no public force-close API, so this fallback narrowly targets BleakClientBlueZDBus private fields; other backends retain their existing public disconnect path. Capture caller diagnostics before spawning the cleanup task.

Scope: four files only—connection lifecycle, two regression-test files, and Linux D-Bus / Python 3.13–3.14 CI setup. Fork packaging and versioning live on codex/preview-ble-disconnect-cleanup.

Validation:

  • 170 tests pass on Python 3.13 and 3.14; all contribution-guide hooks and current GitHub checks pass.
  • 13 regression cases fail on unchanged upstream main; all 16 added cases pass with this patch.
  • All four real-D-Bus cases also fail on release v1.1.2.
  • Real Bleak 3.0.2 / dbus-fast 5.0.22: 100 simulated remote drops leave no additional file descriptors; timeout, EOFError, and OSError release socket, watcher, and monitor.
  • Packaged preview independently passes all 170 tests; all 125 tracked component files match its source commit.
  • Preview 1.1.3b2 deployed to Home Assistant 2026.7.1 with STREAM Ultra and local Realtek 0bda:8771 adapter. Telemetry returned; HA D-Bus connections dropped from 233 to 2.
  • Observed two natural BLE drops: sockets released, automatic reconnect/authentication after about 76 and 43 seconds, socket count back to 2, no HA restart. Fresh grid readings verified afterward. Signal approximately -90 dBm; one transient connection failure occurred before recovery.
  • Configuration data/options and all 72 entity IDs preserved. Upstream's existing migration changed entry minor version 2 to 3.
  • Delta 3 1500 is not currently visible to BlueZ; its connectivity is not validated by this smoke test. Long-term hardware soak remains pending.

Review: fixed lost caller context, added timeout/error cleanup after a separate reproduction, removed duplicate mock-loop coverage and fork-only PR content. Existing SHP3 test-fixture coroutine warnings reproduce unchanged upstream; left untouched.

Related open connection PRs rabits#450, rabits#487, rabits#507, and rabits#509 do not supply this transport cleanup. rabits#507 edits the same disconnect callback; coordinate/rebase if it lands first.

AI-assisted implementation and review. Prepared for upstream draft submission; hardware soak remains in progress.

Preview: https://github.com/kzkvv/ha-ef-ble/releases/tag/v1.1.3b2

@kzkvv kzkvv changed the title Fix BLE disconnect socket leak Close BLE transports after disconnects and timeouts Sep 17, 2026
rabits pushed a commit to rabits/ha-ef-ble that referenced this pull request Oct 3, 2026
Repeated BLE disconnects can leave retired Bleak clients holding D-Bus sockets. On my Linux Home Assistant host, HA held 233 connections when the system bus reached its 256-per-user limit and EcoFlow telemetry stopped.

## Changes

- Retain each client until bounded cleanup finishes, including clients whose BLE link is already disconnected.
- Keep cleanup outside cancelled auth/unload tasks, shield cleanup waiters, wait before reconnecting, and ignore callbacks from old clients.
- On a BlueZ disconnect timeout or error, release the remaining D-Bus transport, device watcher, and disconnect monitor. Preserve the original caller in disconnect diagnostics.

The fallback is limited to `BleakClientBlueZDBus`. It uses private Bleak 3.0.2 fields because there is no public force-close API; compatibility and lifecycle ordering deserve particular review. Other backends retain the public disconnect path.

Scope is four files: connection cleanup, two regression-test suites, and Linux D-Bus / Python 3.13–3.14 CI setup. Preview packaging and version changes are on a separate branch.

## Validation

- 170 tests passed on Python 3.13 and 3.14. The submitted head's fork CI passes Python tests, code style, HACS, and hassfest.
- All 16 added regression cases pass; 13 fail on the unchanged upstream baseline used during implementation. All four real-D-Bus cases also fail on v1.1.2.
- With real Bleak/D-Bus transports, 100 simulated remote drops leave no additional file descriptors; timeout, EOFError, and OSError paths release resources.
- [Preview 1.1.3b2](https://github.com/kzkvv/ha-ef-ble/releases/tag/v1.1.3b2) has been tested on STREAM Ultra with a local Realtek USB adapter since September 17, with repeated checks over about two weeks. HA was subsequently upgraded from 2026.7.1 to 2026.9.4.
- Live checks observed client sockets being released after natural drops and automatic reconnection. The latest October 2 check found two HA D-Bus connections, fresh telemetry, and the deployed preview unchanged.
- Separate Bluetooth connectivity problems occurred earlier in the trial, and adapter startup timeouts remain. Telemetry has been substantially stable since September 26; the main later data gap coincided with a host reboot. This fix does not claim to resolve radio or adapter failures.

See [fork PR #1](kzkvv#1) for the original validation record. Existing SHP3 test-fixture coroutine warnings were reproduced on the unchanged baseline and left out of scope.
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.

1 participant