Commit graph

1 commit

Author SHA1 Message Date
torlando-tech
8cd54443c8 fix: Clean up identity mappings on disconnect to prevent stale connections
Fix stale connection issue where identity mappings persist after disconnect,
preventing automatic reconnection when peer returns with different MAC address.

ROOT CAUSE:
- _device_disconnected_callback() cleaned up spawned_interfaces but NOT:
  - address_to_identity mapping
  - identity_to_address mapping
- handle_central_disconnected() had same issue
- Result: Laptop thinks it's still connected after Android restarts
- Manual rnsd restart required to clear stale state

THE FIX (TDD Approach):
1. RED: Wrote 5 tests demonstrating the bug (all FAILED initially)
2. GREEN: Added identity mapping cleanup to both disconnect methods
3. GREEN: All 5 tests now PASS

Changes:
- BLEInterface.py _device_disconnected_callback():
  - Added del address_to_identity[address]
  - Added del identity_to_address[identity_hash]

- BLEInterface.py handle_central_disconnected():
  - Added del address_to_identity[address]
  - Added del identity_to_address[identity_hash]

- linux_bluetooth_driver.py:
  - Added RNS warning handler for better logging

- tests/test_identity_mapping_cleanup.py (NEW):
  - 5 tests verifying identity mapping cleanup
  - Tests both central and peripheral disconnect modes
  - Reproduces real-world stale connection scenario
  - Verifies automatic reconnection after fix

Test Results:
 All 5 tests PASS after fix
 Mappings properly cleaned up on disconnect
 Automatic reconnection enabled

Impact:
- No more manual rnsd restart needed
- Android MAC rotation handled correctly
- Stale connections automatically cleaned up
- Reconnection works without intervention

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 15:37:54 -05:00