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()
# Wait for table existence, reconnecting to avoid stale transactions
while True:
with engine.connect() as conn: with engine.connect() as conn:
while not db_ready(conn): if db_ready(conn):
break
time.sleep(0.5) time.sleep(0.5)
if time.time() - start > 120: if time.time() - start > 120:
raise TimeoutError("service_config table not available") raise TimeoutError("service_config table not available")
def read_seed() -> Optional[bytes]: def read_seed() -> Optional[bytes]:
# Use a fresh connection/session per read to avoid snapshot staleness
try: try:
with Session(bind=conn) as s: 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
@ -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:
with Session(bind=wconn) as s:
s.add(ServiceConfigValue(key='private_key', packed_value={"value": seed.hex()})) s.add(ServiceConfigValue(key='private_key', packed_value={"value": seed.hex()}))
s.commit() 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: