ble-reticulum/tests
torlando-tech e97e550b4e fix(ble): Add peripheral disconnect cleanup to prevent peer limit blocking
Fixes a critical bug where Android devices (acting as BLE centrals) disconnecting
from Pi GATT servers (acting as peripherals) never triggered cleanup, causing stale
peer entries to accumulate until the 7-peer limit was reached and blocked all new
connections.

## Root Cause
- When centrals disconnected from peripheral mode, no cleanup occurred
- `BLEGATTServer._handle_central_disconnected()` method didn't exist
- `on_central_disconnected` callback was never wired to driver
- No D-Bus signal monitoring for device disconnections
- Stale entries remained in `_peers` dict until daemon restart

## Implementation (TDD Approach)

**New Methods:**
- `LinuxBluetoothDriver._handle_peripheral_disconnected()` (line 852)
  - Removes peer from `_peers` dictionary
  - Notifies on_device_disconnected callback
  - Triggers full cleanup chain in BLEInterface

- `BluezeroGATTServer._handle_central_disconnected()` (line 1945)
  - Removes from `connected_centrals` dictionary
  - Logs connection duration
  - Invokes driver callback

- `BluezeroGATTServer._monitor_device_disconnections()` (line 1645)
  - Monitors D-Bus PropertiesChanged signals
  - Detects when Connected property becomes False
  - Runs in separate daemon thread
  - Automatically triggers cleanup on disconnect

**Callback Wiring:** (line 1558)
`on_central_disconnected = driver._handle_peripheral_disconnected`

## Testing
- Created comprehensive test suite (9 tests, all passing)
- `tests/test_peripheral_disconnect_cleanup.py`:
  - Callback wiring verification
  - Peer dictionary cleanup
  - D-Bus signal handling simulation
  - Edge cases (multiple disconnects, race conditions, shutdown)
  - Reproduces real-world bug from production logs
- No regressions in existing tests (test_bluez_state_cleanup.py passes)

## Current Status
 Core cleanup logic implemented and tested
 Deployed to 4 production devices (10.0.0.80, .242, .39, .246)
⚠️  D-Bus monitoring thread needs debugging (not logging yet)

**Known Issue:** D-Bus signal subscription may need alternative approach.
See PERIPHERAL_DISCONNECT_FIX_SUMMARY.md for troubleshooting steps.

**Fallback Option:** Timeout-based polling can be implemented if D-Bus proves difficult.

Reference: Production logs showed device 4A:87:8C:C7:E3:F3 repeatedly blocked
by "max peers (7) reached" due to uncleaned peripheral disconnections.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-12 19:37:12 -05:00
..
conftest.py Initial commit: BLE Reticulum interface 2025-10-26 19:14:14 -04:00
mock_ble_driver.py Refactor BLEInterface to driver-based architecture 2025-11-03 23:15:22 -05:00
test_ble_peer_interface.py Initial commit: BLE Reticulum interface 2025-10-26 19:14:14 -04:00
test_bleak_threading_hang.py Initial commit: BLE Reticulum interface 2025-10-26 19:14:14 -04:00
test_bleak_with_exec_loading.py Initial commit: BLE Reticulum interface 2025-10-26 19:14:14 -04:00
test_bluez_state_cleanup.py fix(ble): Add BlueZ state cleanup to prevent persistent "Operation already in progress" errors 2025-11-10 00:51:27 -05:00
test_breddr_fallback_prevention.py fix(ble): Clarify ConnectDevice() object path return as success 2025-11-10 19:47:34 -05:00
test_config_directory.py fix: support custom config directories via RNS.Reticulum.configdir 2025-10-28 19:09:48 -04:00
test_error_recovery.py Initial commit: BLE Reticulum interface 2025-10-26 19:14:14 -04:00
test_fragmentation.py Initial commit: BLE Reticulum interface 2025-10-26 19:14:14 -04:00
test_gatt_server.py Initial commit: BLE Reticulum interface 2025-10-26 19:14:14 -04:00
test_gatt_server_readiness.py fix(ble): Add D-Bus verification to prevent GATT server initialization race 2025-11-10 19:51:23 -05:00
test_installer.sh refactor: make libffi-dev conditional for armhf (32-bit ARM) only 2025-10-29 11:02:51 -04:00
test_integration.py test: Add comprehensive v2.2 protocol test suites 2025-11-07 23:00:30 -05:00
test_multi_device_simulation.py Initial commit: BLE Reticulum interface 2025-10-26 19:14:14 -04:00
test_peripheral_disconnect_cleanup.py fix(ble): Add peripheral disconnect cleanup to prevent peer limit blocking 2025-11-12 19:37:12 -05:00
test_prioritization.py test: Update integration tests for driver abstraction refactor 2025-11-07 22:48:38 -05:00
test_scanner_connection_coordination.py fix(ble): Add scanner-connection coordination to prevent "InProgress" errors 2025-11-10 19:44:20 -05:00
test_v2_2_identity_handshake.py test: Add comprehensive v2.2 protocol test suites 2025-11-07 23:00:30 -05:00
test_v2_2_mac_sorting.py test: Add comprehensive v2.2 protocol test suites 2025-11-07 23:00:30 -05:00
test_v2_2_race_conditions.py test: Add comprehensive v2.2 protocol test suites 2025-11-07 23:00:30 -05:00