修复监控握锁卖币导致中控快照超时、实例全部离线。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-07 10:56:40 +08:00
parent 265317672c
commit 066c90fd96
2 changed files with 30 additions and 16 deletions
+27 -5
View File
@@ -375,15 +375,16 @@ def sync_open_options_trades(
pass
if notify_cfg is not None:
_notify_recent_unsent_expiry_closes(conn, notify_cfg)
_sell_spot_after_synced_closes(notify_cfg, closed_inst_ids)
# 卖币不在此处做:调用方须先 commit/close 本连接,再调 sell_spot_after_options_sync,
# 否则监控线程握锁时中控快照会超时,表现为全部离线。
return updated
def _sell_spot_after_synced_closes(
cfg: dict[str, Any],
closed_inst_ids: list[str],
def sell_spot_after_options_sync(
cfg: dict[str, Any] | None,
closed_inst_ids: list[str] | None = None,
) -> None:
"""同步关仓(含到期)后卖回币本位残留;空仓时再扫一遍交易户."""
"""在释放业务 DB 连接后调用:同步关仓/到期后的币本位卖回."""
if not isinstance(cfg, dict):
return
ex = cfg.get("exchange_options")
@@ -540,9 +541,14 @@ def options_monitor_loop(
pe_cfg = dict(profit_exit_cfg or {})
pe_cfg.setdefault("send_wechat", send_wechat)
pe_cfg.setdefault("account_label", account_label)
# 平仓/卖币会另开 DB 连接;先提交释放写锁,避免中控快照被锁超时→全部离线
if target_close_fn is not None:
from lib.options.options_target_lib import run_options_target_closes
try:
conn.commit()
except Exception:
pass
run_options_target_closes(
conn,
positions,
@@ -554,6 +560,10 @@ def options_monitor_loop(
if profit_exit_close_fn is not None:
from lib.options.options_profit_exit_lib import run_options_profit_exits
try:
conn.commit()
except Exception:
pass
run_options_profit_exits(
conn,
positions,
@@ -564,10 +574,22 @@ def options_monitor_loop(
ex=pe_cfg.get("exchange_options"),
)
if sync_trades_fn is not None:
try:
conn.commit()
except Exception:
pass
sync_trades_fn(conn)
conn.commit()
finally:
conn.close()
# 币本位扫尾卖回:必须在监控连接关闭之后
try:
pe_sell = dict(profit_exit_cfg or {})
pe_sell.setdefault("send_wechat", send_wechat)
pe_sell.setdefault("account_label", account_label)
sell_spot_after_options_sync(pe_sell)
except Exception:
pass
# 平仓限价挂单超时撤单(独立于 DB 事务)
if stale_pending_fn is not None:
try: