Skip to content

Commit

Permalink
fix(python): prevent raise condition when sending attachements
Browse files Browse the repository at this point in the history
aiohttp does not support concurrent ws.send_bytes() so guarding that code with a lock.
aio-libs/aiohttp#2934
  • Loading branch information
jourdain committed Aug 13, 2021
1 parent 3c3195e commit 667e68e
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions python/src/wslink/backends/aiohttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def __init__(self, protocol=None, web_app=None):
self.attachmentsReceived = {}
self.attachmentsRecvQueue = []
self.connections = {}
self.attachment_atomic = asyncio.Lock()

# Build the rpc method dictionary, assuming we were given a serverprotocol
if self.getServerProtocol():
Expand Down Expand Up @@ -426,20 +427,28 @@ async def sendWrappedMessage(self, rpcid, content, method="", client_id=None):
found_keys.append(key)
# increment for key
pub.publishManager.registerAttachment(key)
# send header
header = {
"wslink": "1.0",
"method": "wslink.binary.attachment",
"args": [key],
}
json_header = json.dumps(header, ensure_ascii=False)

for key in found_keys:
# send header
header = {
"wslink": "1.0",
"method": "wslink.binary.attachment",
"args": [key],
}
json_header = json.dumps(header, ensure_ascii=False)

# aiohttp can not handle pending ws.send_bytes()
# tried with semaphore but got exception with >1
# https://github.com/aio-libs/aiohttp/issues/2934
async with self.attachment_atomic:
for ws in websockets:
# Send binary header
await ws.send_str(json_header)
# Send binary message
await ws.send_bytes(attachments[key])

# decrement for key
pub.publishManager.unregisterAttachment(key)
# decrement for key
pub.publishManager.unregisterAttachment(key)

pub.publishManager.freeAttachments(keys=found_keys)

Expand Down

0 comments on commit 667e68e

Please sign in to comment.