-
Notifications
You must be signed in to change notification settings - Fork 199
Make sure sequence numbers do not align between zigpy and devices #1636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Conversation
|
The new function will be much slower but it's still reasonably fast. This is an average case comparison, with the RX TSN randomly varying within the range: In [17]: %timeit device.get_sequence()
626 ns ± 4.37 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
In [19]: %timeit device.old_get_sequence()
41 ns ± 2.47 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)Caching pending TSNs will only speed things up by 50ns. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR prevents sequence number collisions between the coordinator and devices by implementing a rotation threshold mechanism. When generating new transaction sequence numbers (TSNs), the coordinator now avoids numbers that are too close to recently received sequence numbers from devices.
- Replaces simple sequential TSN generation with a collision-aware algorithm
- Tracks the last received sequence number from each device
- Implements a 20-number threshold to maintain distance between coordinator and device sequence numbers
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| zigpy/device.py | Implements new TSN generation logic with collision avoidance and device sequence tracking |
| tests/test_zdo.py | Updates test expectations to reflect new TSN generation starting at threshold value |
| tests/test_device.py | Adjusts duplicate request tests for new TSN collision behavior and adds device sequence setup |
|
I have looking little and Zigbee is using sequence counters in all 4 layers: 15.2, Zigbee Network, Zigbee Application and ZCL. That is making me thinking you must do the same for group Seq so they is not colliding with unicast Seq but is depends if you have locked down to the device / endpoint / cluster or is fully open then the sender is the same of the command (you knowing me and reading cod. . .). I think you is on one very good approach that can solve some strange problems in some systems if getting colliding Seq and messing things up !! 2 interesting reflations: And IKEA Dirigera with all updated (Ziogbe 3) devices is one pure Zigbe PRO of lat version (likely 2023) then broadcasts is not being repeated then all routers is using passive broadcast ack (the broadcasting router is listening that all nigglers is repeating OK and not rebroadcast 3 times as old Zogbee is doing). |
|
Thank you for the research!
Right now we only filter duplicate packets in a 10s window if they have the same source, destination (including address mode), endpoints, cluster, and profile. We ignore the APS sequence number. I think this is working and would not be affected by the issue you mentioned above where a device is reusing a TSN. I haven't really looked too much at APS TSNs, to be honest, and I think we should look at how we do retrying: all retries of a request should use the same APS and ZCL sequence numbers, I believe. |
|
By the layer as long doing ZCL all shall being OK only using TSN for that. Great work !!!!! |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #1636 +/- ##
==========================================
- Coverage 99.31% 99.29% -0.02%
==========================================
Files 63 63
Lines 12184 12198 +14
==========================================
+ Hits 12100 12112 +12
- Misses 84 86 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
#1633 is a more "proper" solution but I think this one is more robust.
Essentially, the coordinator and the device maintain separate sequence numbers for outgoing requests. If these happen to overlap at some point, it's possible that a device sends an attribute report with the same sequence number as a response to a request.
To avoid this, we avoid getting too close to the device when generating new TSNs while avoiding our own pending requests.