Skip to content

Commit 87f5c53

Browse files
MaxFreedomPollardteknium1
authored andcommitted
fix(yuanbao): await the forwarded-records loading heartbeat
ForwardedRecordsParseMiddleware.handle() called the coroutine function _send_loading_heartbeat() without awaiting it, so the coroutine was built and dropped. The RUNNING heartbeat never reached the client and Python raised "coroutine was never awaited". Forwarded-record parsing is the slow inbound path, which is where the loading bubble matters most: the user sees nothing while the deep parse runs. Awaiting is safe, since the helper already swallows every exception and the call sits inside the middleware's own try block.
1 parent 7729c18 commit 87f5c53

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎gateway/platforms/yuanbao.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2160,7 +2160,7 @@ class ForwardedRecordsParseMiddleware(InboundMiddleware):
21602160
async def handle(self, ctx: InboundContext, next_fn) -> None:
21612161
try:
21622162
if ctx.forwarded_records:
2163-
self._send_loading_heartbeat(ctx)
2163+
await self._send_loading_heartbeat(ctx)
21642164
ctx.raw_text = self.build_forward_text(ctx.forwarded_records, ctx=ctx, is_dispatch=True)
21652165
except Exception as exc:
21662166
# Degrade gracefully: leave ctx.raw_text as-is.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""ForwardedRecordsParseMiddleware must actually send its loading heartbeat.
2+
3+
``_send_loading_heartbeat`` is a coroutine function. Calling it without
4+
``await`` builds a coroutine object and drops it, so the RUNNING heartbeat is
5+
never sent and Python raises "coroutine was never awaited" at collection time.
6+
Forwarded-record parsing is the slow inbound path, which is exactly where the
7+
user needs the loading bubble.
8+
"""
9+
10+
import warnings
11+
12+
import pytest
13+
14+
from gateway.platforms.yuanbao import (
15+
ForwardedRecordsParseMiddleware,
16+
InboundContext,
17+
WS_HEARTBEAT_RUNNING,
18+
)
19+
20+
21+
class _Heartbeat:
22+
def __init__(self):
23+
self.calls = []
24+
25+
async def send_heartbeat_once(self, chat_id, state):
26+
self.calls.append((chat_id, state))
27+
28+
29+
class _Outbound:
30+
def __init__(self):
31+
self.heartbeat = _Heartbeat()
32+
33+
34+
class _Adapter:
35+
name = "yuanbao"
36+
37+
def __init__(self):
38+
self._outbound = _Outbound()
39+
40+
41+
@pytest.mark.asyncio
42+
async def test_forwarded_records_send_the_loading_heartbeat():
43+
adapter = _Adapter()
44+
ctx = InboundContext(adapter=adapter)
45+
ctx.chat_id = "chat-1"
46+
ctx.forwarded_records = [{"msgContent": {"text": "hello"}}]
47+
48+
called = []
49+
50+
async def _next():
51+
called.append(True)
52+
53+
with warnings.catch_warnings():
54+
# A dropped coroutine surfaces here as a RuntimeWarning rather than a
55+
# failure; promote it so the regression cannot pass silently.
56+
warnings.simplefilter("error", RuntimeWarning)
57+
await ForwardedRecordsParseMiddleware().handle(ctx, _next)
58+
59+
assert adapter._outbound.heartbeat.calls == [("chat-1", WS_HEARTBEAT_RUNNING)]
60+
assert called, "middleware must still call next_fn"
61+
62+
63+
@pytest.mark.asyncio
64+
async def test_no_forwarded_records_sends_no_heartbeat():
65+
adapter = _Adapter()
66+
ctx = InboundContext(adapter=adapter)
67+
ctx.chat_id = "chat-1"
68+
69+
async def _next():
70+
pass
71+
72+
await ForwardedRecordsParseMiddleware().handle(ctx, _next)
73+
74+
assert adapter._outbound.heartbeat.calls == []

0 commit comments

Comments
 (0)