fix(task): stop canceled runs and clear stale task events

This commit is contained in:
JOJO 2026-04-04 19:46:56 +08:00
parent b9df354808
commit 0296418cff
2 changed files with 13 additions and 0 deletions

View File

@ -577,6 +577,16 @@ async def handle_task_with_sender(terminal: WebTerminal, workspace: UserWorkspac
iteration = 0
while max_iterations is None or iteration < max_iterations:
# 检查停止标志
stop_entry = get_stop_flag(client_sid, username)
if stop_entry and stop_entry.get('stop'):
debug_log(f"[Task] 检测到停止标志,退出循环")
sender('task_stopped', {
'message': '任务已停止',
'reason': 'user_requested'
})
break
current_iteration = iteration + 1
iteration += 1
total_iterations += 1

View File

@ -177,6 +177,9 @@ class TaskManager:
with self._lock:
rec.status = "cancel_requested"
rec.updated_at = time.time()
# 清空事件队列,防止新任务读取到旧事件
rec.events.clear()
debug_log(f"[Task] 任务 {task_id} 已取消,事件队列已清空")
return True
# ---- internal helpers ----