Repository navigation
Serialize multicast table subscribe/unsubscribe - #771
Merged
Merged
Conversation
The subscription cache is only updated after the firmware write is awaited, so concurrent subscribe() calls for the same group (e.g. several devices initializing at once and requesting the same group) each popped a free slot and wrote it, leaking a table slot and overwriting the cached slot reference. Guard subscribe/unsubscribe with a lock.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #771 +/- ##
=======================================
Coverage 99.59% 99.59%
=======================================
Files 64 64
Lines 4409 4417 +8
=======================================
+ Hits 4391 4399 +8
Misses 18 18 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Multicast.subscribe()only records a group in its cache after the firmware write is awaited, but it claims a free table slot before that await. Two concurrent calls for the same group both miss the cache, each pop a slot and write it, and the second cache write overwrites the first slot reference. One multicast table slot is leaked per race, and the table is small (bellows asks for 16).This became reachable with zigpy's
subscribe_to_multicast_group()(zigpy/zigpy#1864) and the quirk-side API in zigpy/zha-device-handlers#5209: ZHA initializes devices concurrently, so several devices sharing a quirk (e.g. two BILRESA remotes) request the same group at the same time.Changes
Multicast.subscribe()andMulticast.unsubscribe()with oneasyncio.Lock, so check, slot claim, firmware write and cache update are atomic with respect to each other. The already-subscribed early return then handles the second caller.startup()is unchanged and does not hold the lock, so there is no re-entrancy.Testing
test_subscribe_concurrent_same_group(two concurrent subscribes use one slot and unsubscribing frees it) andtest_subscribe_unsubscribe_serialized. Both fail without the lock.pre-commitclean.