修复粉尘桥拦开仓,并继续消除模拟盘嵌套握锁导致的 database is locked。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-07 11:28:29 +08:00
parent 030b237981
commit ea3e029eb9
7 changed files with 196 additions and 33 deletions
+58 -27
View File
@@ -522,14 +522,32 @@ def options_monitor_loop(
if stop_event is not None and getattr(stop_event, "is_set", lambda: False)():
break
try:
conn = get_db()
# 持仓拉取不握 DB,避免与模拟盘钱包写互相锁
try:
if cond_order_tick_fn is not None:
positions = fetch_positions()
except Exception:
positions = []
pe_cfg = dict(profit_exit_cfg or {})
pe_cfg.setdefault("send_wechat", send_wechat)
pe_cfg.setdefault("account_label", account_label)
# 条件单可能嵌套开仓/卖币:单独短连接
if cond_order_tick_fn is not None:
conn = get_db()
try:
cond_order_tick_fn(conn)
conn.commit()
except Exception:
pass
finally:
try:
cond_order_tick_fn(conn)
conn.close()
except Exception:
pass
positions = fetch_positions()
conn = get_db()
try:
run_options_profit_alerts(
conn,
positions,
@@ -538,17 +556,22 @@ def options_monitor_loop(
account_label=account_label,
ticker_bid_fn=ticker_bid_fn,
)
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
conn.commit()
except Exception:
pass
finally:
try:
conn.close()
except Exception:
pass
try:
conn.commit()
except Exception:
pass
# 目标/翻倍出场:短连接内扫描;close_fn 在连接关闭后由内部仍可能嵌套—
# 此处每段自管连接,平仓前必 close
if target_close_fn is not None:
from lib.options.options_target_lib import run_options_target_closes
conn = get_db()
try:
run_options_target_closes(
conn,
positions,
@@ -557,13 +580,20 @@ def options_monitor_loop(
account_label=account_label,
cfg=pe_cfg,
)
if profit_exit_close_fn is not None:
from lib.options.options_profit_exit_lib import run_options_profit_exits
conn.commit()
except Exception:
pass
finally:
try:
conn.commit()
conn.close()
except Exception:
pass
if profit_exit_close_fn is not None:
from lib.options.options_profit_exit_lib import run_options_profit_exits
conn = get_db()
try:
run_options_profit_exits(
conn,
positions,
@@ -573,23 +603,24 @@ def options_monitor_loop(
cfg=pe_cfg,
ex=pe_cfg.get("exchange_options"),
)
conn.commit()
finally:
conn.close()
# 同步/卖币必须在监控连接关闭之后
conn.commit()
except Exception:
pass
finally:
try:
conn.close()
except Exception:
pass
if sync_trades_fn is not None:
try:
sync_trades_fn(None)
except Exception:
pass
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)
sell_spot_after_options_sync(pe_cfg)
except Exception:
pass
# 平仓限价挂单超时撤单(独立于 DB 事务)
if stale_pending_fn is not None:
try:
stale_pending_fn()