Skip to content

Commit 667e68e

Browse files
committed
fix(python): prevent raise condition when sending attachements
aiohttp does not support concurrent ws.send_bytes() so guarding that code with a lock. aio-libs/aiohttp#2934
1 parent 3c3195e commit 667e68e

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

python/src/wslink/backends/aiohttp/__init__.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ def __init__(self, protocol=None, web_app=None):
160160
self.attachmentsReceived = {}
161161
self.attachmentsRecvQueue = []
162162
self.connections = {}
163+
self.attachment_atomic = asyncio.Lock()
163164

164165
# Build the rpc method dictionary, assuming we were given a serverprotocol
165166
if self.getServerProtocol():
@@ -426,20 +427,28 @@ async def sendWrappedMessage(self, rpcid, content, method="", client_id=None):
426427
found_keys.append(key)
427428
# increment for key
428429
pub.publishManager.registerAttachment(key)
429-
# send header
430-
header = {
431-
"wslink": "1.0",
432-
"method": "wslink.binary.attachment",
433-
"args": [key],
434-
}
435-
json_header = json.dumps(header, ensure_ascii=False)
430+
431+
for key in found_keys:
432+
# send header
433+
header = {
434+
"wslink": "1.0",
435+
"method": "wslink.binary.attachment",
436+
"args": [key],
437+
}
438+
json_header = json.dumps(header, ensure_ascii=False)
439+
440+
# aiohttp can not handle pending ws.send_bytes()
441+
# tried with semaphore but got exception with >1
442+
# https://github.com/aio-libs/aiohttp/issues/2934
443+
async with self.attachment_atomic:
436444
for ws in websockets:
445+
# Send binary header
437446
await ws.send_str(json_header)
438447
# Send binary message
439448
await ws.send_bytes(attachments[key])
440449

441-
# decrement for key
442-
pub.publishManager.unregisterAttachment(key)
450+
# decrement for key
451+
pub.publishManager.unregisterAttachment(key)
443452

444453
pub.publishManager.freeAttachments(keys=found_keys)
445454

0 commit comments

Comments
 (0)