Skip to content

Commit 7fc6713

Browse files
committed
Fix concurrency
1 parent 8db1703 commit 7fc6713

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

bellows/zigbee/application.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -863,18 +863,18 @@ async def send_packet(self, packet: zigpy.types.ZigbeePacket) -> None:
863863
extended_timeout = False
864864

865865
async with self._limit_concurrency(priority=packet.priority):
866-
async with self._req_lock:
867-
message_tag = self.get_sequence()
868-
pending_tag = (packet.dst.address, message_tag)
866+
message_tag = self.get_sequence()
867+
pending_tag = (packet.dst.address, message_tag)
869868

870-
if pending_tag in self._pending_requests:
871-
raise zigpy.exceptions.DeliveryError(
872-
f"Packet with tag {pending_tag} is already pending, cannot send"
873-
)
869+
if pending_tag in self._pending_requests:
870+
raise zigpy.exceptions.DeliveryError(
871+
f"Packet with tag {pending_tag} is already pending, cannot send"
872+
)
874873

875-
future = self._pending_requests[pending_tag] = asyncio.Future()
874+
future = self._pending_requests[pending_tag] = asyncio.Future()
876875

877-
try:
876+
try:
877+
async with self._req_lock:
878878
if packet.dst.addr_mode == zigpy.types.AddrMode.NWK:
879879
if device is not None:
880880
await self._ezsp.set_extended_timeout(
@@ -925,31 +925,31 @@ async def send_packet(self, packet: zigpy.types.ZigbeePacket) -> None:
925925
data=packet.data.serialize(),
926926
)
927927

928-
if status != t.sl_Status.OK:
929-
raise zigpy.exceptions.DeliveryError(
930-
f"Failed to enqueue message: {status!r}", status
931-
)
928+
if status != t.sl_Status.OK:
929+
raise zigpy.exceptions.DeliveryError(
930+
f"Failed to enqueue message: {status!r}", status
931+
)
932932

933-
# Only throw a delivery exception for packets sent with NWK addressing.
934-
# https://github.com/home-assistant/core/issues/79832
935-
# Broadcasts/multicasts don't have ACKs or confirmations either.
936-
if packet.dst.addr_mode != zigpy.types.AddrMode.NWK:
937-
return
938-
939-
# Wait for `messageSentHandler` message
940-
async with asyncio_timeout(
941-
MESSAGE_SEND_TIMEOUT_MAINS
942-
if not packet.extended_timeout
943-
else MESSAGE_SEND_TIMEOUT_BATTERY
944-
):
945-
send_status, _ = await future
946-
947-
if t.sl_Status.from_ember_status(send_status) != t.sl_Status.OK:
948-
raise zigpy.exceptions.DeliveryError(
949-
f"Failed to deliver message: {send_status!r}", send_status
950-
)
951-
finally:
952-
del self._pending_requests[pending_tag]
933+
# Only throw a delivery exception for packets sent with NWK addressing.
934+
# https://github.com/home-assistant/core/issues/79832
935+
# Broadcasts/multicasts don't have ACKs or confirmations either.
936+
if packet.dst.addr_mode != zigpy.types.AddrMode.NWK:
937+
return
938+
939+
# Wait for `messageSentHandler` message
940+
async with asyncio_timeout(
941+
MESSAGE_SEND_TIMEOUT_MAINS
942+
if not packet.extended_timeout
943+
else MESSAGE_SEND_TIMEOUT_BATTERY
944+
):
945+
send_status, _ = await future
946+
947+
if t.sl_Status.from_ember_status(send_status) != t.sl_Status.OK:
948+
raise zigpy.exceptions.DeliveryError(
949+
f"Failed to deliver message: {send_status!r}", send_status
950+
)
951+
finally:
952+
del self._pending_requests[pending_tag]
953953

954954
async def permit(self, time_s: int = 60, node: t.EmberNodeId = None) -> None:
955955
"""Permit joining."""

0 commit comments

Comments
 (0)