[Bug] baostock loader has no timeout; baostock's client can block or spin forever when its server stops answering
- Interface: MCP Plugin
- LLM Provider: n/a
- Version: 0.1.15. The same code is on
mainate5f7195. The baostock version is 0.9.3. - Environment: macOS (Darwin 25.6.0) / Python 3.14.5
Description
What happened:
baostock's client has no read timeout. baostock/util/socketutil.py opens its socket without settimeout (SocketUtil.connect(), lines 34-41). send_msg then reads like this (lines 67-73):
while True:
recv = default_socket.recv(8192)
receive += recv
if receive[-13:] == b"<![CDATA[]]>\n":
break- Silent server. If the server stops sending,
recvblocks forever. - Closed connection. If the peer closes the connection,
recvreturnsb""forever and the loop spins at 100% CPU. - Errors swallowed.
send_msgalso wraps everything inexcept Exception as ex: print(ex)(lines 89-90).
vibe-trading adds no deadline around any of this:
- The loader.
BaostockLoader.fetchrunsbs.login(), then_fetch_one, thenbs.logout()with no time limit (backtest/loaders/baostock_loader.py:86-110). - The fallback chain.
_fetch_via_chainhas no per-attempt timeout. - The MCP call. MCP
get_market_datagoes throughToolRegistry.execute(mcp_server.py:1832,src/agent/tools.py:97), and that has no timeout either.
The backtest path is bounded, because Runner(timeout=300) runs loaders in a subprocess (src/tools/backtest_tool.py:57, src/core/runner.py:431,636). get_market_data is not bounded.
What I expected: A stalled source should fail within a bounded time, and the fallback chain should move on.
Steps to Reproduce
The fault is server-side and intermittent. We observed it on 2026-09-18 from about 23:46 (UTC+8), calling baostock 0.9.3 directly. This is the same package vibe-trading imports.
bs.login()returnederror_code "0"in about 0.5 s.bs.query_history_k_data_plus("sh.600000", "date,close", start_date="2026-09-01", end_date="2026-09-18", frequency="d", adjustflag="3")never returned.- Two such processes stayed at about 100% CPU for over 2 minutes, until we killed them. Their CPU time roughly equalled wall-clock time.
- Fresh single sessions at 23:48 and 23:57 each exceeded a 30 s guard.
- Between stalls the service answered normally, in 0.6-2.2 s. It later stalled twice more against a 60 s guard, the last time after midnight on a full-history query.
- On 2026-09-19 at about 10:49 (UTC+8), login again succeeded and the same small query again exceeded a 30 s guard. So the stalls are not limited to late night.
Error Logs
There are none. The call simply never returns.
Suggested fix
- Bound every baostock call with a wall-clock deadline, and treat expiry as a loader failure so
_fetch_via_chaincontinues. - A worker subprocess or process pool is the robust option. A thread cannot be killed and would keep spinning.
- SIGALRM only works on the main thread. Our local script used it, raising a
BaseExceptionsubclass so baostock'sexcept Exceptioncould not swallow it, and it cut stalls off at 60 s. That approach is not usable inside a threaded MCP server. - Setting
socket.settimeoutonbaostock.common.context.default_socketafter login should cover the silent-server case. It would not cover the spin after a closed connection. That part needs a fix in baostock itself: break on an emptyrecv. Neither of these is tested.
Observed vs inferred
- Observed: the stalls and the CPU spin in the baostock client itself, and the absence of any timeout in the code paths listed above.
- Inferred, not observed: an MCP
get_market_datacall routed to baostock during such a stall would hang indefinitely. We deliberately avoided triggering this. - How baostock gets reached. An explicit
source="baostock"reaches it directly. Withsource="auto", baostock is fourth in the A-share chain after tencent, mootdx and eastmoney (registry.py:159). So it is only reached after the earlier sources fail. One such failure is Tencent answering HTTP 501 when it rate-limits, which we saw for over 20 minutes the same evening.
Source: HKUDS/Vibe-Trading