2424from services .config import DATA_DIR
2525from services .json_file import read_json_file , write_json_file
2626from services .proxy_service import proxy_settings
27+ from utils .sqlite_runtime import sqlite_connection_guard
2728
2829DDG_ALIASES_FILE = DATA_DIR / "ddg_aliases.json"
2930_ddg_aliases_lock = Lock ()
@@ -185,7 +186,7 @@ def _load_outlook_token_state() -> dict[str, dict[str, Any]]:
185186
186187 The legacy JSON file is imported once into SQLite on first access.
187188 """
188- with closing (_outlook_state_connection ()) as connection :
189+ with sqlite_connection_guard , closing (_outlook_state_connection ()) as connection :
189190 _ensure_outlook_state_migrated (connection )
190191 rows = connection .execute (
191192 "SELECT email, state, reason, updated_at, lease_token FROM mailbox_states"
@@ -202,7 +203,7 @@ def _load_outlook_token_state() -> dict[str, dict[str, Any]]:
202203
203204
204205def _save_outlook_token_state (state : dict [str , dict [str , Any ]]) -> None :
205- with closing (_outlook_state_connection ()) as connection :
206+ with sqlite_connection_guard , closing (_outlook_state_connection ()) as connection :
206207 _ensure_outlook_state_migrated (connection )
207208 connection .execute ("BEGIN IMMEDIATE" )
208209 connection .execute ("DELETE FROM mailbox_states" )
@@ -230,7 +231,7 @@ def _claim_outlook_credential(pool: list[dict[str, str]]) -> tuple[dict[str, str
230231 """Atomically reserve one available credential across worker processes."""
231232 now = datetime .now (timezone .utc )
232233 lease_token = secrets .token_urlsafe (24 )
233- with closing (_outlook_state_connection ()) as connection :
234+ with sqlite_connection_guard , closing (_outlook_state_connection ()) as connection :
234235 _ensure_outlook_state_migrated (connection )
235236 connection .execute ("BEGIN IMMEDIATE" )
236237 rows = connection .execute (
@@ -320,7 +321,7 @@ def _set_outlook_token_state(
320321 target = str (address or "" ).strip ().lower ()
321322 if not target :
322323 return False
323- with closing (_outlook_state_connection ()) as connection :
324+ with sqlite_connection_guard , closing (_outlook_state_connection ()) as connection :
324325 _ensure_outlook_state_migrated (connection )
325326 connection .execute ("BEGIN IMMEDIATE" )
326327 if lease_token :
@@ -363,7 +364,7 @@ def _release_outlook_token_state(address: str, *, lease_token: str = "") -> bool
363364 target = str (address or "" ).strip ().lower ()
364365 if not target :
365366 return False
366- with closing (_outlook_state_connection ()) as connection :
367+ with sqlite_connection_guard , closing (_outlook_state_connection ()) as connection :
367368 _ensure_outlook_state_migrated (connection )
368369 connection .execute ("BEGIN IMMEDIATE" )
369370 if lease_token :
@@ -390,7 +391,7 @@ def clear_outlook_token_states(addresses: list[str] | set[str], states: set[str]
390391 targets .discard ("" )
391392 if not targets :
392393 return 0
393- with closing (_outlook_state_connection ()) as connection :
394+ with sqlite_connection_guard , closing (_outlook_state_connection ()) as connection :
394395 _ensure_outlook_state_migrated (connection )
395396 connection .execute ("BEGIN IMMEDIATE" )
396397 placeholders = "," .join ("?" for _ in targets )
@@ -422,7 +423,7 @@ def reset_outlook_token_pool_state(scope: str = "all") -> int:
422423 target_states = OUTLOOK_BUSY_STATES
423424 else :
424425 target_states = set ()
425- with closing (_outlook_state_connection ()) as connection :
426+ with sqlite_connection_guard , closing (_outlook_state_connection ()) as connection :
426427 _ensure_outlook_state_migrated (connection )
427428 connection .execute ("BEGIN IMMEDIATE" )
428429 if target_states :
0 commit comments