Skip to content

Commit

Permalink
Match pending messages using the destination in addition to the tag (#…
Browse files Browse the repository at this point in the history
…605)

* Match pending messages using the destination in addition to the tag

* Fix tests

* enhance logging to use the tuple in _pending

---------

Co-authored-by: David Mulcahey <david.mulcahey@me.com>
  • Loading branch information
puddly and dmulcahey committed Dec 31, 2023
1 parent 758803d commit d3ba62e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
12 changes: 7 additions & 5 deletions bellows/zigbee/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,20 +711,21 @@ def _handle_frame_sent(
cnt_name = f"unknown_msg_type_{msg}"

try:
request = self._pending[message_tag]
pending_tag = (destination, message_tag)
request = self._pending[pending_tag]
request.result.set_result((status, f"message send {msg}"))
self.state.counters[COUNTERS_CTRL][cnt_name].increment()
except KeyError:
self.state.counters[COUNTERS_CTRL][f"{cnt_name}_unexpected"].increment()
LOGGER.debug("Unexpected message send notification tag: %s", message_tag)
LOGGER.debug("Unexpected message send notification tag: %s", pending_tag)
except asyncio.InvalidStateError as exc:
self.state.counters[COUNTERS_CTRL][f"{cnt_name}_duplicate"].increment()
LOGGER.debug(
(
"Invalid state on future for message tag %s "
"- probably duplicate response: %s"
),
message_tag,
pending_tag,
exc,
)

Expand Down Expand Up @@ -846,7 +847,8 @@ async def send_packet(self, packet: zigpy.types.ZigbeePacket) -> None:

async with self._limit_concurrency():
message_tag = self.get_sequence()
with self._pending.new(message_tag) as req:
pending_tag = (packet.dst.address, message_tag)
with self._pending.new(pending_tag) as req:
for attempt, retry_delay in enumerate(RETRY_DELAYS):
async with self._req_lock:
if packet.dst.addr_mode == zigpy.types.AddrMode.NWK:
Expand Down Expand Up @@ -901,7 +903,7 @@ async def send_packet(self, packet: zigpy.types.ZigbeePacket) -> None:
if attempt < len(RETRY_DELAYS):
LOGGER.debug(
"Request %s failed to enqueue, retrying in %ss: %s",
message_tag,
pending_tag,
retry_delay,
status,
)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def test_frame_handler_ignored(app, aps_frame):
),
)
def test_send_failure(app, aps, ieee, msg_type):
req = app._pending[254] = MagicMock()
req = app._pending[(0xBEED, 254)] = MagicMock()
app.ezsp_callback_handler(
"messageSentHandler", [msg_type, 0xBEED, aps, 254, sentinel.status, b""]
)
Expand All @@ -501,7 +501,7 @@ def test_send_failure(app, aps, ieee, msg_type):


def test_dup_send_failure(app, aps, ieee):
req = app._pending[254] = MagicMock()
req = app._pending[(0xBEED, 254)] = MagicMock()
req.result.set_result.side_effect = asyncio.InvalidStateError()
app.ezsp_callback_handler(
"messageSentHandler",
Expand Down Expand Up @@ -533,7 +533,7 @@ def test_send_failure_unexpected(app, aps, ieee):


def test_send_success(app, aps, ieee):
req = app._pending[253] = MagicMock()
req = app._pending[(0xBEED, 253)] = MagicMock()
app.ezsp_callback_handler(
"messageSentHandler",
[
Expand All @@ -558,7 +558,7 @@ def test_unexpected_send_success(app, aps, ieee):


def test_dup_send_success(app, aps, ieee):
req = app._pending[253] = MagicMock()
req = app._pending[(0xBEED, 253)] = MagicMock()
req.result.set_result.side_effect = asyncio.InvalidStateError()
app.ezsp_callback_handler(
"messageSentHandler",
Expand Down

0 comments on commit d3ba62e

Please sign in to comment.