修复页面握库拉余额导致 database is locked 整页 500。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1619,8 +1619,9 @@ def account_risk_status(conn):
|
||||
ensure_account_risk_schema,
|
||||
)
|
||||
|
||||
ensure_account_risk_schema(conn)
|
||||
now = app_now()
|
||||
try:
|
||||
ensure_account_risk_schema(conn)
|
||||
st = compute_account_risk_status(
|
||||
conn,
|
||||
trading_day=get_trading_day(),
|
||||
@@ -1635,6 +1636,21 @@ def account_risk_status(conn):
|
||||
count_position_limit_active_monitors(conn),
|
||||
max_active_positions=MAX_ACTIVE_POSITIONS,
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"[account_risk_status] {e}", flush=True)
|
||||
return {
|
||||
"enabled": True,
|
||||
"status": "normal",
|
||||
"status_label": "正常",
|
||||
"can_trade": True,
|
||||
"reason": "",
|
||||
"cooloff_until_ms": None,
|
||||
"cooloff_until": None,
|
||||
"manual_close_count": 0,
|
||||
"daily_loss_count": 0,
|
||||
"daily_frozen": False,
|
||||
"freeze_remaining_sec": 0,
|
||||
}
|
||||
|
||||
|
||||
def hub_user_initiated_close(
|
||||
@@ -3863,6 +3879,11 @@ def render_main_page(page="options", embed_mode=None):
|
||||
conn = get_db()
|
||||
session_row = ensure_session(conn, trading_day)
|
||||
local_current_capital = float(session_row["current_capital"])
|
||||
try:
|
||||
conn.commit()
|
||||
except Exception:
|
||||
pass
|
||||
conn.close()
|
||||
from lib.instance.instance_embed_context_lib import (
|
||||
embed_render_plan,
|
||||
minimal_stats_bundle,
|
||||
@@ -3875,6 +3896,7 @@ def render_main_page(page="options", embed_mode=None):
|
||||
)
|
||||
|
||||
plan = embed_render_plan(page, embed_mode)
|
||||
# 拉交易所余额前不持库连接,避免 database is locked → 页面 500
|
||||
if plan.exchange_capitals:
|
||||
funding_capital, trading_capital = get_exchange_capitals()
|
||||
else:
|
||||
@@ -3938,6 +3960,7 @@ def render_main_page(page="options", embed_mode=None):
|
||||
options_margin_mode = "coin"
|
||||
options_underly = "ETH"
|
||||
recommended_capital = get_recommended_capital(capital_for_sizing)
|
||||
conn = get_db()
|
||||
key_list = (
|
||||
conn.execute("SELECT * FROM key_monitors").fetchall() if plan.key_list else []
|
||||
)
|
||||
@@ -4328,7 +4351,13 @@ def api_account_snapshot():
|
||||
conn = get_db()
|
||||
session_row = ensure_session(conn, trading_day)
|
||||
local_current_capital = float(session_row["current_capital"])
|
||||
try:
|
||||
conn.commit()
|
||||
except Exception:
|
||||
pass
|
||||
conn.close()
|
||||
force_refresh = (request.args.get("force") or "").strip().lower() in ("1", "true", "yes")
|
||||
# 拉交易所余额前不持库连接
|
||||
funding_capital, trading_capital = get_exchange_capitals(force=force_refresh)
|
||||
funding_usdt = round(funding_capital, FUNDS_DECIMALS) if funding_capital is not None else None
|
||||
# 顶栏只展示交易所余额;勿用会话资金回退(避免模拟盘 30U 残留到实盘)
|
||||
@@ -4372,6 +4401,7 @@ def api_account_snapshot():
|
||||
options_margin_mode = "coin"
|
||||
options_underly = "ETH"
|
||||
recommended_capital = get_recommended_capital(capital_for_sizing)
|
||||
conn = get_db()
|
||||
from lib.trade.trade_labels_lib import count_position_limit_active_monitors
|
||||
|
||||
position_limit_count = count_position_limit_active_monitors(conn)
|
||||
|
||||
@@ -275,12 +275,15 @@ def _load_state(conn):
|
||||
|
||||
|
||||
def _sync_trading_day(conn, trading_day: str, now: Optional[datetime] = None) -> Any:
|
||||
import sqlite3
|
||||
|
||||
row = _load_state(conn)
|
||||
td = (trading_day or "").strip()
|
||||
stored = str(_row_get(row, "trading_day") or "").strip()
|
||||
if stored != td:
|
||||
now_ms = _now_ms(now)
|
||||
cooloff_active = _resolved_cooloff_until_ms(row, now_ms)
|
||||
try:
|
||||
conn.execute(
|
||||
"""UPDATE account_risk_state SET
|
||||
trading_day=?,
|
||||
@@ -302,6 +305,9 @@ def _sync_trading_day(conn, trading_day: str, now: Optional[datetime] = None) ->
|
||||
),
|
||||
)
|
||||
row = _load_state(conn)
|
||||
except sqlite3.OperationalError:
|
||||
# 页面渲染勿因跨日写库锁死而 500;下次再同步
|
||||
pass
|
||||
return row
|
||||
|
||||
|
||||
@@ -800,16 +806,9 @@ def apply_position_limit_risk(
|
||||
return out
|
||||
|
||||
|
||||
def compute_account_risk_status(
|
||||
conn,
|
||||
*,
|
||||
trading_day: str,
|
||||
now: Optional[datetime] = None,
|
||||
fmt_local_ms: Optional[Callable[[int], str]] = None,
|
||||
) -> dict[str, Any]:
|
||||
if not risk_control_enabled():
|
||||
def _disabled_or_fallback_risk_status(*, enabled: bool = False) -> dict[str, Any]:
|
||||
return {
|
||||
"enabled": False,
|
||||
"enabled": bool(enabled),
|
||||
"status": STATUS_NORMAL,
|
||||
"status_label": STATUS_LABELS[STATUS_NORMAL],
|
||||
"can_trade": True,
|
||||
@@ -819,7 +818,24 @@ def compute_account_risk_status(
|
||||
"manual_close_count": 0,
|
||||
"daily_loss_count": 0,
|
||||
"daily_frozen": False,
|
||||
"daily_loss_limit": daily_loss_limit() if enabled else 0,
|
||||
"pending_journal_trade_id": None,
|
||||
"freeze_remaining_sec": 0,
|
||||
}
|
||||
|
||||
|
||||
def compute_account_risk_status(
|
||||
conn,
|
||||
*,
|
||||
trading_day: str,
|
||||
now: Optional[datetime] = None,
|
||||
fmt_local_ms: Optional[Callable[[int], str]] = None,
|
||||
) -> dict[str, Any]:
|
||||
import sqlite3
|
||||
|
||||
if not risk_control_enabled():
|
||||
return _disabled_or_fallback_risk_status(enabled=False)
|
||||
try:
|
||||
row = _sync_trading_day(conn, trading_day, now=now)
|
||||
now_ms = _now_ms(now)
|
||||
daily_frozen = int(_row_get(row, "daily_frozen") or 0) == 1
|
||||
@@ -859,10 +875,8 @@ def compute_account_risk_status(
|
||||
remaining_ms = cooloff_until_ms - now_ms
|
||||
hours = _cooloff_hours_value(row)
|
||||
status = _freeze_tier_from_remaining_ms(remaining_ms, hours)
|
||||
status_label = _freeze_status_label(hours, status)
|
||||
until_str = _ms_to_local_str(cooloff_until_ms, fmt_local_ms) if fmt_local_ms else None
|
||||
label = status_label
|
||||
reason = f"账户{label}中"
|
||||
reason = f"账户{_freeze_status_label(hours, status)}中"
|
||||
if until_str:
|
||||
reason += f",至 {until_str}"
|
||||
|
||||
@@ -889,6 +903,9 @@ def compute_account_risk_status(
|
||||
"pending_journal_trade_id": pending,
|
||||
"freeze_remaining_sec": freeze_remaining_sec if not can_trade else 0,
|
||||
}
|
||||
except sqlite3.OperationalError:
|
||||
# 渲染路径遇 database is locked 时降级为正常,避免整页 500
|
||||
return _disabled_or_fallback_risk_status(enabled=True)
|
||||
|
||||
|
||||
def account_risk_blocks_trading(
|
||||
|
||||
Reference in New Issue
Block a user