Skip to content

Commit

Permalink
fix(pyra2yr): Race condition in WebSocketClient
Browse files Browse the repository at this point in the history
In send_message(), when retrieving result from out_queue, another task
could put a message to in_queue, resulting in wrong message to be
retrieved from out_queue.

Fixed by introducing a lock.

Closes #5
  • Loading branch information
shmocz committed Oct 3, 2023
1 parent da0a6cf commit 80809e9
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pyra2yr/pyra2yr/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def __init__(self, uri: str, timeout=5.0):
self.task = None
self._tries = 15
self._connect_delay = 1.0
self._lock = asyncio.Lock()

def open(self):
self.task = asyncio.create_task(async_log_exceptions(self.main()))
Expand All @@ -103,8 +104,9 @@ async def close(self):
await self.task

async def send_message(self, m: str) -> aiohttp.WSMessage:
await self.in_queue.put(m)
return await self.out_queue.get()
async with self._lock:
await self.in_queue.put(m)
return await self.out_queue.get()

async def main(self):
# send the initial message
Expand Down

0 comments on commit 80809e9

Please sign in to comment.