Z User commited on
Commit
2a76983
·
1 Parent(s): 460aac4

fix: WeChat send_document timeout - remove cross-event-loop session reuse

Browse files

send_weixin_direct() in weixin.py reused the gateway's aiohttp.ClientSession
when called from send_message_tool via _run_async() (worker thread with a
different event loop). aiohttp 3.13+ uses asyncio.timeout() internally,
which raises 'Timeout context manager should be used inside a task' when
the session's loop doesn't match the caller's loop.

Fix: remove the live_adapter session reuse path, always create a fresh
session for one-shot sends (the fallback path at line 2058+ already does
this correctly).

Files changed (2) hide show
  1. Dockerfile +6 -0
  2. scripts/patch_weixin_cross_loop.py +93 -0
Dockerfile CHANGED
@@ -40,6 +40,12 @@ RUN python3 /tmp/patch_auto_media.py; rm -f /tmp/patch_auto_media.py
40
  COPY scripts/patch_resolve_media_paths.py /tmp/patch_resolve_media_paths.py
41
  RUN python3 /tmp/patch_resolve_media_paths.py; rm -f /tmp/patch_resolve_media_paths.py
42
 
 
 
 
 
 
 
43
  # Install Node.js 23
44
  RUN ARCH=$(dpkg --print-architecture) \
45
  && if [ "$ARCH" = "amd64" ]; then NODE_ARCH="x64"; else NODE_ARCH="$ARCH"; fi \
 
40
  COPY scripts/patch_resolve_media_paths.py /tmp/patch_resolve_media_paths.py
41
  RUN python3 /tmp/patch_resolve_media_paths.py; rm -f /tmp/patch_resolve_media_paths.py
42
 
43
+ # Patch: Fix WeixinAdapter cross-event-loop session reuse
44
+ # Prevents "Timeout context manager should be used inside a task" when
45
+ # send_weixin_direct() reuses the gateway's aiohttp session from _run_async()
46
+ COPY scripts/patch_weixin_cross_loop.py /tmp/patch_weixin_cross_loop.py
47
+ RUN python3 /tmp/patch_weixin_cross_loop.py; rm -f /tmp/patch_weixin_cross_loop.py
48
+
49
  # Install Node.js 23
50
  RUN ARCH=$(dpkg --print-architecture) \
51
  && if [ "$ARCH" = "amd64" ]; then NODE_ARCH="x64"; else NODE_ARCH="$ARCH"; fi \
scripts/patch_weixin_cross_loop.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Patch hermes-agent WeixinAdapter to fix cross-event-loop session reuse.
3
+
4
+ ROOT CAUSE: send_weixin_direct() in weixin.py reuses the live adapter's
5
+ aiohttp.ClientSession (created in the gateway's event loop) when called
6
+ from _run_async() (which creates a new event loop in a worker thread).
7
+ aiohttp 3.13+ uses asyncio.timeout() internally, which raises
8
+ "Timeout context manager should be used inside a task" when the session's
9
+ event loop doesn't match the caller's loop.
10
+
11
+ FIX: In send_weixin_direct(), skip the live_adapter reuse path entirely
12
+ and always create a fresh aiohttp session + adapter. This function is only
13
+ called from send_message_tool._send_weixin() via _run_async() (a different
14
+ event loop), so reusing the gateway's session is always wrong here.
15
+ """
16
+
17
+ import sys
18
+ import os
19
+ import glob
20
+
21
+
22
+ def patch_file(filepath: str):
23
+ with open(filepath, 'r') as f:
24
+ content = f.read()
25
+
26
+ # The problematic block: reuses live_adapter._send_session across event loops
27
+ old = ''' live_adapter = _LIVE_ADAPTERS.get(resolved_token)
28
+ send_session = getattr(live_adapter, '_send_session', None)
29
+ if live_adapter is not None and send_session is not None and not send_session.closed:
30
+ last_result: Optional[SendResult] = None
31
+ cleaned = live_adapter.format_message(message)
32
+ if cleaned:
33
+ last_result = await live_adapter.send(chat_id, cleaned)
34
+ if not last_result.success:
35
+ return {"error": f"Weixin send failed: {last_result.error}"}
36
+
37
+ for media_path, _is_voice in media_files or []:
38
+ ext = Path(media_path).suffix.lower()
39
+ if ext in {".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"}:
40
+ last_result = await live_adapter.send_image_file(chat_id, media_path)
41
+ else:
42
+ last_result = await live_adapter.send_document(chat_id, media_path)
43
+ if not last_result.success:
44
+ return {"error": f"Weixin media send failed: {last_result.error}"}
45
+
46
+ return {
47
+ "success": True,
48
+ "platform": "weixin",
49
+ "chat_id": chat_id,
50
+ "message_id": last_result.message_id if last_result else None,
51
+ "context_token_used": bool(context_token),
52
+ }'''
53
+
54
+ new = ''' # NOTE: Do NOT reuse live_adapter._send_session here.
55
+ # send_weixin_direct() is called from _run_async() (model_tools.py) which
56
+ # creates a new event loop in a worker thread. The live adapter\'s
57
+ # aiohttp.ClientSession was created in the gateway\'s event loop, and
58
+ # aiohttp 3.13+ raises "Timeout context manager should be used inside a
59
+ # task" when the session\'s loop doesn\'t match the caller\'s loop.
60
+ # Always fall through to the fresh-session path below.'''
61
+
62
+ if old not in content:
63
+ print(f"WARNING: Could not find live_adapter reuse block in {filepath}", file=sys.stderr)
64
+ print("The upstream code may have changed. Skipping this patch.", file=sys.stderr)
65
+ sys.exit(0)
66
+
67
+ content = content.replace(old, new, 1)
68
+
69
+ with open(filepath, 'w') as f:
70
+ f.write(content)
71
+
72
+ print(f"Patched {filepath}: removed cross-event-loop session reuse in send_weixin_direct()")
73
+
74
+
75
+ if __name__ == "__main__":
76
+ candidates = [
77
+ "/app/hermes-agent/gateway/platforms/weixin.py",
78
+ ]
79
+ candidates.extend(glob.glob("/app/venv/lib/**/gateway/platforms/weixin.py", recursive=True))
80
+
81
+ filepath = None
82
+ for c in candidates:
83
+ if os.path.isfile(c):
84
+ filepath = c
85
+ break
86
+
87
+ if not filepath:
88
+ print("WARNING: weixin.py not found in any candidate location", file=sys.stderr)
89
+ print(f"Checked: {candidates}", file=sys.stderr)
90
+ print("Skipping patch_weixin_cross_loop.", file=sys.stderr)
91
+ sys.exit(0)
92
+
93
+ patch_file(filepath)