fix secrets read stuck

This commit is contained in:
user 2025-08-24 19:39:28 +03:00
parent 4401916104
commit 7d920907cc
1 changed files with 19 additions and 17 deletions

View File

@ -41,15 +41,20 @@ def _init_seed_via_db() -> bytes:
# Wait for table to exist # Wait for table to exist
start = time.time() start = time.time()
with engine.connect() as conn: # Wait for table existence, reconnecting to avoid stale transactions
while not db_ready(conn): while True:
time.sleep(0.5) with engine.connect() as conn:
if time.time() - start > 120: if db_ready(conn):
raise TimeoutError("service_config table not available") break
time.sleep(0.5)
if time.time() - start > 120:
raise TimeoutError("service_config table not available")
def read_seed() -> Optional[bytes]: def read_seed() -> Optional[bytes]:
try: # Use a fresh connection/session per read to avoid snapshot staleness
with Session(bind=conn) as s: try:
with engine.connect() as rconn:
with Session(bind=rconn) as s:
row = s.query(ServiceConfigValue).filter(ServiceConfigValue.key == 'private_key').first() row = s.query(ServiceConfigValue).filter(ServiceConfigValue.key == 'private_key').first()
if not row: if not row:
return None return None
@ -58,8 +63,8 @@ def _init_seed_via_db() -> bytes:
packed = json.loads(packed) packed = json.loads(packed)
seed_hex = packed.get('value') seed_hex = packed.get('value')
return bytes.fromhex(seed_hex) if seed_hex else None return bytes.fromhex(seed_hex) if seed_hex else None
except Exception: except Exception:
return None return None
seed = read_seed() seed = read_seed()
if seed: if seed:
@ -69,16 +74,13 @@ def _init_seed_via_db() -> bytes:
seed = _load_seed_from_env_or_generate() seed = _load_seed_from_env_or_generate()
# Try insert; if another primary raced, ignore # Try insert; if another primary raced, ignore
try: try:
with Session(bind=conn) as s: with engine.connect() as wconn:
s.add(ServiceConfigValue(key='private_key', packed_value={"value": seed.hex()})) with Session(bind=wconn) as s:
s.commit() s.add(ServiceConfigValue(key='private_key', packed_value={"value": seed.hex()}))
s.commit()
make_log("HotWallet", "Seed saved in service_config by primary", level='info') make_log("HotWallet", "Seed saved in service_config by primary", level='info')
return seed return seed
except Exception: except Exception:
try:
conn.rollback()
except Exception:
pass
# Read again in case of race # Read again in case of race
seed2 = read_seed() seed2 = read_seed()
if seed2: if seed2: