Skip to content

Commit

Permalink
Fix reconnect race (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jan 5, 2022
1 parent 670f464 commit afe15a9
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions flux_led/aiodevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __init__(
) -> None:
"""Init and setup the bulb."""
super().__init__(ipaddr, port, timeout, discovery)
self._lock = asyncio.Lock()
self._connect_lock = asyncio.Lock()
self._aio_protocol: Optional[AIOLEDENETProtocol] = None
self._get_time_lock: asyncio.Lock = asyncio.Lock()
self._get_time_future: Optional[asyncio.Future[bool]] = None
Expand Down Expand Up @@ -747,8 +747,10 @@ def process_remote_config_response(self, msg: bytes) -> None:
async def _async_send_msg(self, msg: bytearray) -> None:
"""Write a message on the socket."""
if not self._aio_protocol:
async with self._lock:
await self._async_connect()
async with self._connect_lock:
# Check again under the lock
if not self._aio_protocol:
await self._async_connect()
assert self._aio_protocol is not None
self._aio_protocol.write(msg)

Expand All @@ -758,7 +760,7 @@ async def _async_determine_protocol(self) -> None:
protocol = protocol_cls()
assert isinstance(protocol, (ProtocolLEDENET8Byte, ProtocolLEDENETOriginal))
self._protocol = protocol
async with self._lock:
async with self._connect_lock:
await self._async_connect()
assert self._aio_protocol is not None
self._determine_protocol_future = asyncio.Future()
Expand Down

0 comments on commit afe15a9

Please sign in to comment.