修复模拟盘卖币握锁导致 database is locked 与中控超时离线。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-07 11:04:06 +08:00
parent 066c90fd96
commit 80ef5ceccb
7 changed files with 173 additions and 126 deletions
+58 -41
View File
@@ -437,8 +437,36 @@ def _sync_options_trades(
raw = raw_positions
live_ids = {str(p.get("instId") or "") for p in raw if str(p.get("instId") or "")}
def _hist(inst_id: str):
return fetch_option_position_history(ex, inst_id)
# 网络拉取历史必须在 DB 事务外,否则握锁期间卖币/中控快照会 database is locked
pending_insts: list[str] = []
conn = cfg["get_db"]()
try:
init_options_tables(conn)
rows = conn.execute(
"SELECT inst_id FROM options_trades WHERE status = 'open'"
).fetchall()
for r in rows:
iid = str(r["inst_id"] or "").strip()
if iid and iid not in live_ids:
pending_insts.append(iid)
try:
conn.commit()
except Exception:
pass
finally:
conn.close()
hist_by_inst: dict[str, list] = {}
for iid in pending_insts:
try:
hist_by_inst[iid] = fetch_option_position_history(ex, iid) or []
except Exception:
hist_by_inst[iid] = []
hist_all = None
try:
hist_all = fetch_all_option_positions_history(ex, limit=200)
except Exception:
hist_all = None
conn = cfg["get_db"]()
try:
@@ -447,17 +475,25 @@ def _sync_options_trades(
sync_open_options_trades(
conn,
live_inst_ids=live_ids,
fetch_history_fn=_hist,
fetch_history_fn=lambda inst_id: hist_by_inst.get(str(inst_id) or [], []),
notify_cfg=cfg,
)
if hist_all is not None:
try:
backfill_closed_options_realized_pnl_from_history(conn, hist_all)
except Exception:
pass
conn.commit()
except Exception:
try:
hist_all = fetch_all_option_positions_history(ex, limit=200)
backfill_closed_options_realized_pnl_from_history(conn, hist_all)
conn.rollback()
except Exception:
pass
conn.commit()
finally:
conn.close()
try:
conn.close()
except Exception:
pass
try:
from lib.options.options_monitor_lib import sell_spot_after_options_sync
@@ -1703,23 +1739,15 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
inst_id = (data.get("inst_id") or "").strip() or None
from lib.options.options_spot_bridge_lib import sell_residual_after_option_flat
conn = cfg["get_db"]()
try:
init_options_tables(conn)
try:
conn.commit()
except Exception:
pass
out = sell_residual_after_option_flat(conn, ex, underlying=underlying, inst_id=inst_id)
try:
conn.commit()
except Exception:
pass
if out.get("ok"):
_mark_balances_stale(cfg)
return jsonify(out)
finally:
conn.close()
out = sell_residual_after_option_flat(
cfg["get_db"],
ex,
underlying=underlying,
inst_id=inst_id,
)
if out.get("ok"):
_mark_balances_stale(cfg)
return jsonify(out)
@app.route("/api/options/convert/quote", methods=["POST"])
@lr
@@ -1979,24 +2007,13 @@ def _start_monitor_thread(app: Flask, cfg: dict[str, Any]) -> None:
return []
return [cfg["format_position_row"](p) for p in raw]
def _sync(conn):
from lib.exchange.okx_options_lib import fetch_option_position_history
from lib.options.options_monitor_lib import reconcile_live_open_trades, sync_open_options_trades
ex = cfg.get("exchange_options")
if ex is None:
def _sync(_conn=None):
# 不用监控线程传入的连接;force=False 走 15s 节流,卖币由监控循环在关闭连接后另扫
try:
_sync_options_trades(cfg, force=False)
return 1
except Exception:
return 0
raw = cfg["fetch_option_positions"](ex)
if raw is None:
return 0
live_ids = {str(p.get("instId") or "") for p in raw if str(p.get("instId") or "")}
reconcile_live_open_trades(conn, live_inst_ids=live_ids)
return sync_open_options_trades(
conn,
live_inst_ids=live_ids,
fetch_history_fn=lambda inst_id: fetch_option_position_history(ex, inst_id),
notify_cfg=cfg,
)
def _target_close(inst_id: str) -> dict[str, Any]:
from lib.options.options_target_lib import close_option_by_bid_depth