From ae81626731504bb882d2267a9ffcc92af05b8d15 Mon Sep 17 00:00:00 2001 From: Jeason00011 <123796609+Jeason00011@users.noreply.github.com> Date: Thu, 7 May 2026 20:47:35 +0800 Subject: [PATCH] fix: handle queue.Empty in task mode to prevent crash on LLM timeout The reflect mode already catches this exception, but task mode did not, causing an unhandled crash when LLM response exceeds 120s between chunks. Co-Authored-By: Claude Opus 4.7 --- agentmain.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/agentmain.py b/agentmain.py index 7c4a8a86..30754465 100644 --- a/agentmain.py +++ b/agentmain.py @@ -209,9 +209,13 @@ def run(self): with open(infile, encoding='utf-8') as f: raw = f.read() while True: dq = agent.put_task(raw, source='task') - while 'done' not in (item := dq.get(timeout=300)): - if 'next' in item and random.random() < 0.95: # 概率写一次中间结果 - with open(f'{d}/output{nround}.txt', 'w', encoding='utf-8') as f: f.write(item.get('next', '')) + try: + while 'done' not in (item := dq.get(timeout=300)): + if 'next' in item and random.random() < 0.95: # 概率写一次中间结果 + with open(f'{d}/output{nround}.txt', 'w', encoding='utf-8') as f: f.write(item.get('next', '')) + except queue.Empty: + with open(f'{d}/output{nround}.txt', 'w', encoding='utf-8') as f: f.write('[ERROR] LLM response timeout (300s)\n\n[ROUND END]\n') + break with open(f'{d}/output{nround}.txt', 'w', encoding='utf-8') as f: f.write(item['done'] + '\n\n[ROUND END]\n') consume_file(d, '_stop') # 已经成功停下来了,避免打断下次reply for _ in range(300): # 等reply.txt,10分钟超时