fix(loaders): preserve bounded BaoStock 0.9.4 connections

This commit is contained in:
Haozhe Wu committed 2026-10-10 23:24:28 +08:00
1 parent 6965c2884b
commit 8e430079fd
3 files changed
+30 -4

No files matched your search

+2
View File
@@ -35,6 +35,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
#1666, #1665); align Vitest with its coverage plugin. Keep `uuid-utils` at
0.17.0 because the proposed 1.0.0 upgrade (#1670) conflicts with the current
LangChain constraint. Preserve mootdx's retirement from the public canary.
BaoStock's bounded connection wrapper accepts the new 0.9.4 API-key argument
while retaining public/VIP routing and compatibility with 0.9.3.
### Maintenance — 2026-10-09
+10 -4
View File
@@ -103,13 +103,19 @@ def _bounded_send_msg(msg: str, timeout: float) -> Optional[str]:
return None
def _connect_with_timeout(self, timeout: float) -> None:
"""SocketUtil.connect with a deadline and no unbound-socket NameError."""
def _connect_with_timeout(self, timeout: float, api_key: str = "0") -> None:
"""Bound SDK 0.9.3/0.9.4 connections while preserving SDK host routing.
Args:
timeout: Connection deadline in seconds.
api_key: SDK 0.9.4 routing argument; omitted by SDK 0.9.3.
"""
import baostock.common.contants as cons
import baostock.common.context as context
try:
sock = socket.create_connection((cons.BAOSTOCK_SERVER_IP, cons.BAOSTOCK_SERVER_PORT), timeout=timeout)
host = cons.BAOSTOCK_VIP_SERVER_IP if api_key.startswith("bs-") else cons.BAOSTOCK_SERVER_IP
sock = socket.create_connection((host, cons.BAOSTOCK_SERVER_PORT), timeout=timeout)
except OSError as exc:
logger.warning("baostock connect failed: %s", exc)
sock = None
@@ -134,7 +140,7 @@ def _baostock_socket_guard(timeout: float):
previous_socket = getattr(context, "default_socket", None)
context.default_socket = None
socketutil.send_msg = lambda msg: _bounded_send_msg(msg, timeout)
socketutil.SocketUtil.connect = lambda util: _connect_with_timeout(util, timeout)
socketutil.SocketUtil.connect = lambda util, api_key="0": _connect_with_timeout(util, timeout, api_key)
try:
yield True
finally:
@@ -135,6 +135,24 @@ def test_guard_patches_and_restores() -> None:
assert socketutil.SocketUtil.connect is original_connect
@pytest.mark.parametrize("args,vip", [((), False), (("0",), False), (("bs-test",), True)])
def test_guard_accepts_legacy_and_api_key_connect(monkeypatch, args, vip) -> None:
"""Keep SDK 0.9.3/0.9.4 routing and deadlines through the same guard."""
from unittest.mock import Mock
monkeypatch.setattr(cons, "BAOSTOCK_VIP_SERVER_IP", "vip.example", raising=False)
sock = Mock()
create = Mock(return_value=sock)
monkeypatch.setattr(socket, "create_connection", create)
with _baostock_socket_guard(0.5) as acquired:
assert acquired
bs.util.socketutil.SocketUtil().connect(*args)
host = cons.BAOSTOCK_VIP_SERVER_IP if vip else cons.BAOSTOCK_SERVER_IP
create.assert_called_once_with((host, cons.BAOSTOCK_SERVER_PORT), timeout=0.5)
assert context.default_socket is sock
sock.close.assert_called_once_with()
def test_read_timeout_env(monkeypatch) -> None:
assert _read_timeout() == _DEFAULT_READ_TIMEOUT
monkeypatch.setenv(_READ_TIMEOUT_ENV, "0.7")