Skip to content

Commit bbdb26f

Browse files
authored
Merge pull request #234 from tina4stack/fix/ws-realtime-event-loop-blocking
perf(ws,realtime): offload blocking backplane publish + chat/upload I/O off the event loop
2 parents 996efd5 + a959f4d commit bbdb26f

3 files changed

Lines changed: 114 additions & 7 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright (c) 2026 Code Infinity
2+
# SPDX-License-Identifier: MPL-2.0
3+
# This Source Code Form is subject to the terms of the Mozilla Public
4+
# License, v. 2.0. If a copy of the MPL was not distributed with this
5+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
6+
7+
"""THE WEBSOCKET BACKPLANE PUBLISH MUST NOT BLOCK THE EVENT LOOP.
8+
9+
The Redis backplane's LISTENER already runs in its own thread
10+
(RedisBackplane.subscribe -> pubsub.run_in_thread). The PUBLISH side does not:
11+
`WebSocketManager.broadcast*` (async, on the loop) calls `_publish`, which calls
12+
the synchronous `redis.publish` inline. So every broadcast - a WS send, a WebRTC
13+
signalling relay (broadcast_to_room), a chat fan-out - makes a blocking socket
14+
round-trip ON the loop, freezing every other connection for its duration.
15+
16+
Proven the ADR-0074 way: a real WebSocketManager, a real clock and threads, and
17+
a real backplane object whose `publish` carries a measured latency (the sleep is
18+
the instrument, not a mock of Redis). No local connections, so the broadcast is
19+
purely the publish. RED before the offload, GREEN after.
20+
"""
21+
import asyncio
22+
import time
23+
24+
from tina4_python.websocket import WebSocketManager
25+
26+
DELAY = 0.3
27+
N = 4
28+
29+
30+
class SlowBackplane:
31+
"""A real backplane whose publish blocks for DELAY (stands in for a slow
32+
Redis round-trip). subscribe is a no-op; the test only broadcasts."""
33+
34+
def publish(self, channel, message):
35+
time.sleep(DELAY)
36+
37+
def subscribe(self, channel, callback):
38+
pass
39+
40+
def close(self):
41+
pass
42+
43+
44+
def _manager():
45+
mgr = WebSocketManager()
46+
mgr._backplane_started = True # skip the real wiring in _ensure_backplane
47+
mgr._backplane = SlowBackplane()
48+
return mgr
49+
50+
51+
def test_backplane_publish_does_not_stall_the_event_loop():
52+
"""While one broadcast's publish runs, the loop must keep ticking."""
53+
mgr = _manager()
54+
ticks = []
55+
56+
async def ticker():
57+
for _ in range(200):
58+
ticks.append(time.monotonic())
59+
await asyncio.sleep(0.01)
60+
61+
async def drive():
62+
t = asyncio.ensure_future(ticker())
63+
await asyncio.sleep(0)
64+
await mgr.broadcast_all("hello")
65+
t.cancel()
66+
67+
asyncio.run(drive())
68+
69+
assert len(ticks) > 15, (
70+
f"the loop ticked only {len(ticks)} times during one broadcast's "
71+
f"{DELAY}s publish. The backplane publish ran ON the loop, so every "
72+
"connection was frozen with it."
73+
)
74+
75+
76+
def test_concurrent_broadcasts_do_not_serialize_on_the_publish():
77+
"""N concurrent broadcasts must overlap their publishes, not queue on them.
78+
79+
Serial (today): N * DELAY. Concurrent (after offload): ~DELAY.
80+
"""
81+
mgr = _manager()
82+
83+
async def drive():
84+
start = time.monotonic()
85+
await asyncio.gather(*(mgr.broadcast_all(f"m{i}") for i in range(N)))
86+
return time.monotonic() - start
87+
88+
elapsed = asyncio.run(drive())
89+
serial = N * DELAY
90+
assert elapsed < serial * 0.6, (
91+
f"{N} concurrent broadcasts took {elapsed:.2f}s; serial would be ~{serial:.2f}s "
92+
f"and concurrent ~{DELAY:.2f}s. They serialised on the backplane publish."
93+
)

‎tina4_python/realtime/__init__.py‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,11 @@ async def chat_ws(connection, event, data):
333333
body = (payload.get("body") or "").strip()
334334
if not body:
335335
return
336-
saved = _persist_message(channel_id, identity, body,
337-
payload.get("thread_id"))
336+
# _persist_message does a blocking ORM write; run it off the loop
337+
# so one chat message never freezes every other WS connection.
338+
saved = await asyncio.to_thread(
339+
_persist_message, channel_id, identity, body,
340+
payload.get("thread_id"))
338341
if saved is not None:
339342
# Deliver to everyone in the room INCLUDING the sender, so
340343
# the sender's optimistic message is reconciled with its
@@ -380,7 +383,9 @@ async def files_upload(request, response):
380383
upload = request.files.get("file")
381384
if not upload:
382385
return response({"error": "no file uploaded (field 'file')"}, 400)
383-
saved = _store_upload(store, channel_id, upload)
386+
# _store_upload writes to the storage backend (local file or S3);
387+
# run it off the loop so an upload never freezes other requests.
388+
saved = await asyncio.to_thread(_store_upload, store, channel_id, upload)
384389
saved["url"] = store.url(saved["key"]) or \
385390
f"{paths['files']}/{saved['key']}"
386391
return response(saved, 201)
@@ -476,14 +481,20 @@ def _persist_message(channel_id, identity, body, thread_id):
476481
async def _mark_read(channel_id, identity):
477482
"""Advance the member's read cursor to now (read receipt)."""
478483
from tina4_python.realtime.models import ChannelMember
479-
try:
484+
485+
def _advance_cursor():
480486
# where() takes a WHERE fragment; select_one() takes full SQL.
481487
members = ChannelMember.where(
482488
"channel_id = ? AND user_id = ?", [channel_id, identity], limit=1)
483489
if members:
484490
member = members[0]
485491
member.last_read_at = _now_iso()
486492
member.save()
493+
494+
try:
495+
# The read + write are blocking ORM calls; run them off the loop so a
496+
# read receipt never freezes other WS connections.
497+
await asyncio.to_thread(_advance_cursor)
487498
except Exception as exc:
488499
Log.error(f"realtime chat: read-receipt update failed: {exc}")
489500

‎tina4_python/websocket/__init__.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,10 @@ async def broadcast(self, message: str | bytes, exclude: str = None, path: str =
624624
and not await self._safe_send(ws, message)]
625625
for ws in dead:
626626
self.remove(ws)
627-
self._publish("path" if path else "all", message, path=path, exclude=exclude)
627+
# The backplane publish is a blocking socket round-trip (synchronous
628+
# redis.publish); run it off the loop so a broadcast never freezes every
629+
# other connection. The listener side is already threaded.
630+
await asyncio.to_thread(self._publish, "path" if path else "all", message, path=path, exclude=exclude)
628631

629632
async def broadcast_all(self, message: str | bytes):
630633
"""Send message to ALL connections (resilient to dead clients).
@@ -635,7 +638,7 @@ async def broadcast_all(self, message: str | bytes):
635638
if not await self._safe_send(ws, message)]
636639
for ws in dead:
637640
self.remove(ws)
638-
self._publish("all", message)
641+
await asyncio.to_thread(self._publish, "all", message)
639642

640643
async def send_to(self, client_id: str, message: str | bytes):
641644
"""Send a message to a specific client by ID.
@@ -736,7 +739,7 @@ async def broadcast_to_room(self, room_name: str, message: str | bytes,
736739
and not await self._safe_send(ws, message)]
737740
for ws in dead:
738741
self.remove(ws)
739-
self._publish("room", message, room=room_name, exclude=exclude)
742+
await asyncio.to_thread(self._publish, "room", message, room=room_name, exclude=exclude)
740743

741744

742745
class WebSocketServer:

0 commit comments

Comments
 (0)