fix(simulation): guarantee SQLite connection close with finally block

This commit is contained in:
Ubuntu 2026-04-26 14:56:52 +00:00
parent 117eabf607
commit 75533e9319
2 changed files with 25 additions and 21 deletions

View File

@ -258,7 +258,7 @@ class IPCHandler:
agent_prompts = {} # 记录每个agent的prompt agent_prompts = {} # 记录每个agent的prompt
for interview in interviews: for interview in interviews:
agent_id = interview.get("agent_id") agent_id = interview.get("agent_id") or 0
prompt = interview.get("prompt", "") prompt = interview.get("prompt", "")
try: try:
@ -310,6 +310,7 @@ class IPCHandler:
if not os.path.exists(db_path): if not os.path.exists(db_path):
return result return result
conn = None
try: try:
conn = sqlite3.connect(db_path) conn = sqlite3.connect(db_path)
cursor = conn.cursor() cursor = conn.cursor()
@ -333,10 +334,11 @@ class IPCHandler:
except json.JSONDecodeError: except json.JSONDecodeError:
result["response"] = info_json result["response"] = info_json
conn.close()
except Exception as e: except Exception as e:
print(f" 读取Interview结果失败: {e}") print(f" 读取Interview结果失败: {e}")
finally:
if conn:
conn.close()
return result return result

View File

@ -258,7 +258,7 @@ class IPCHandler:
agent_prompts = {} # 记录每个agent的prompt agent_prompts = {} # 记录每个agent的prompt
for interview in interviews: for interview in interviews:
agent_id = interview.get("agent_id") agent_id = interview.get("agent_id") or 0
prompt = interview.get("prompt", "") prompt = interview.get("prompt", "")
try: try:
@ -310,6 +310,7 @@ class IPCHandler:
if not os.path.exists(db_path): if not os.path.exists(db_path):
return result return result
conn = None
try: try:
conn = sqlite3.connect(db_path) conn = sqlite3.connect(db_path)
cursor = conn.cursor() cursor = conn.cursor()
@ -333,10 +334,11 @@ class IPCHandler:
except json.JSONDecodeError: except json.JSONDecodeError:
result["response"] = info_json result["response"] = info_json
conn.close()
except Exception as e: except Exception as e:
print(f" 读取Interview结果失败: {e}") print(f" 读取Interview结果失败: {e}")
finally:
if conn:
conn.close()
return result return result