Fix CTP vnctptd segfault restart loop by serializing reconnect.

Skip duplicate auto-connect when TD is logged in, stop aggressive query_position hooks, and throttle position refresh.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-26 00:49:17 +08:00
parent 9f48f22d16
commit 7133a0e448
4 changed files with 114 additions and 210 deletions
+11 -2
View File
@@ -80,6 +80,7 @@ from trading_context import (
)
from ctp_symbol import ths_to_vnpy_symbol
from vnpy_bridge import (
_ctp_td_lock,
ctp_cancel_order,
ctp_connect,
ctp_get_account,
@@ -1029,7 +1030,8 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
mode = get_trading_mode(get_setting)
if not fast and ctp_status(mode).get("connected"):
try:
get_bridge().refresh_positions()
with _ctp_td_lock:
get_bridge().refresh_positions()
except Exception as exc:
logger.debug("refresh positions before snapshot: %s", exc)
conn = get_db()
@@ -2240,8 +2242,15 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
notify_fn=send_wechat_msg,
interval=1,
)
_pos_refresh_tick = {"n": 0}
def _position_worker_refresh() -> dict:
_pos_refresh_tick["n"] += 1
# 每秒轻量刷新;每 5 秒做一次 CTP 持仓/挂单对账,避免频繁 query 导致 vnctptd 崩溃
return _refresh_trading_live_snapshot(fast=(_pos_refresh_tick["n"] % 5 != 0))
start_position_worker(
refresh_fn=lambda: _refresh_trading_live_snapshot(fast=False),
refresh_fn=_position_worker_refresh,
interval=1,
)
_bootstrap_trading_runtime()