docs: add comment about potential race condition in identity cache

Document the narrow race window where data could arrive from an old MAC
address before onAddressChanged callback invalidates the cache entry.
The window is very small since onAddressChanged fires synchronously
during Kotlin deduplication, and _address_changed_callback() cleans up
the stale cache entry.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
torlando-tech 2026-01-18 00:31:56 -05:00
commit 1e1023f914

View file

@ -1893,6 +1893,12 @@ class BLEInterface(Interface):
if not peer_identity:
# Try identity cache - peer may have "disconnected" from Python's view
# but Android/driver layer maintains the GATT connection
#
# POTENTIAL RACE CONDITION: If MAC rotation occurred and data arrives from
# the OLD address before onAddressChanged callback fires, we could restore
# a stale mapping here. This is a very narrow window since onAddressChanged
# is invoked synchronously from Kotlin during deduplication. The cache entry
# for the old address gets cleaned up in _address_changed_callback().
cached = self._identity_cache.get(peer_address)
if cached and (time.time() - cached[1]) < self._identity_cache_ttl:
peer_identity = cached[0]