[Bug] baostock loader has no timeout; baostock's client can block or spin forever when its server stops answering

Author: jw232Created Sep 19, 2026Updated Sep 19, 2026
  • Interface: MCP Plugin
  • LLM Provider: n/a
  • Version: 0.1.15. The same code is on main at e5f7195. 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):

python
while True:
    recv = default_socket.recv(8192)
    receive += recv
    if receive[-13:] == b"<![CDATA[]]>\n":
        break
  • Silent server. If the server stops sending, recv blocks forever.
  • Closed connection. If the peer closes the connection, recv returns b"" forever and the loop spins at 100% CPU.
  • Errors swallowed. send_msg also wraps everything in except Exception as ex: print(ex) (lines 89-90).

vibe-trading adds no deadline around any of this:

  • The loader. BaostockLoader.fetch runs bs.login(), then _fetch_one, then bs.logout() with no time limit (backtest/loaders/baostock_loader.py:86-110).
  • The fallback chain. _fetch_via_chain has no per-attempt timeout.
  • The MCP call. MCP get_market_data goes through ToolRegistry.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.

  1. bs.login() returned error_code "0" in about 0.5 s.
  2. 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.
  3. Two such processes stayed at about 100% CPU for over 2 minutes, until we killed them. Their CPU time roughly equalled wall-clock time.
  4. Fresh single sessions at 23:48 and 23:57 each exceeded a 30 s guard.
  5. 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.
  6. 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_chain continues.
  • 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 BaseException subclass so baostock's except Exception could not swallow it, and it cut stalls off at 60 s. That approach is not usable inside a threaded MCP server.
  • Setting socket.settimeout on baostock.common.context.default_socket after 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 empty recv. 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_data call 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. With source="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.