Fix platform worker task not appearing in list
Docker Build and Push / build-and-push (cuda, 11.8) (push) Has been skipped
Docker Build and Push / check-secrets (push) Successful in 5s
Docker Build and Push / build-and-push (cpu, latest) (push) Has been skipped
Docker Build and Push / build-and-push (cuda, 12.6) (push) Has been skipped
Docker Build and Push / build-and-push (cuda, 12.8) (push) Has been skipped
Docker Build and Push / build-and-push (directml, latest) (push) Has been skipped
Build Windows CPU / build (push) Has been cancelled
Build Windows CUDA 11.8 / build (push) Has been cancelled
Build Windows CUDA 12.6 / build (push) Has been cancelled
Build Windows CUDA 12.8 / build (push) Has been cancelled
Build Windows DirectML / build (push) Has been cancelled

Two root causes:
1. Task was created in memory but not broadcast to frontend until the first
   update_task() call. If an exception happened before that, the task was
   invisible forever. Fix: broadcast immediately after task creation.
2. run_in_executor had no try/except — any exception was silently swallowed
   by asyncio.create_task with no log. Fix: wrap in try/except/finally,
   log exception via logger.exception, mark task as error.
Also: get_event_loop() → get_running_loop() (correct API for async context).

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
liuchen
2026-06-27 15:30:52 +08:00
co-authored by Claude Haiku 4.5
parent b81f6afd0f
commit b4b9837dde
+36 -26
View File
@@ -694,36 +694,41 @@ class VSRTaskWorker:
cache_path = url_to_cache_path(video_url)
cached = cache_path.exists()
new_task = {
"id": task_id,
"url": video_url,
"is_cache_file": False,
"cache_path": str(cache_path),
"inpaint_mode": ptask.get("inpaint_mode", "sttn-auto"),
"subtitle_areas": ptask.get("subtitle_areas", []),
"preview_mode": ptask.get("preview_mode", False),
"detect_mode": ptask.get("detect_mode", "text"),
"status": "pending",
"progress": 0,
"phase": "pending",
"detect_pct": 0, "process_pct": 0, "merge_pct": 0,
"download_progress": 0,
"cached": cached,
"output_path": None,
"error": None,
"platform_task_id": platform_task_id,
"logs": [],
"created_at": time.time(),
"started_at": None,
"completed_at": None,
}
with task_lock:
tasks[task_id] = {
"id": task_id,
"url": video_url,
"is_cache_file": False,
"cache_path": str(cache_path),
"inpaint_mode": ptask.get("inpaint_mode", "sttn-auto"),
"subtitle_areas": ptask.get("subtitle_areas", []),
"preview_mode": ptask.get("preview_mode", False),
"detect_mode": ptask.get("detect_mode", "text"),
"status": "pending",
"progress": 0,
"phase": "pending",
"detect_pct": 0, "process_pct": 0, "merge_pct": 0,
"download_progress": 0,
"cached": cached,
"output_path": None,
"error": None,
"platform_task_id": platform_task_id,
"logs": [],
"created_at": time.time(),
"started_at": None,
"completed_at": None,
}
tasks[task_id] = new_task
# 立即广播给前端,使任务出现在列表中
_save_task_to_db(new_task)
_push(_broadcast({"type": "task_update", "task": _task_snapshot(new_task)}))
# 在线程中同步执行任务
def _do():
_run_task(task_id)
loop = asyncio.get_event_loop()
loop = asyncio.get_running_loop()
# 进度上报后台 loop
async def _progress_reporter():
@@ -742,8 +747,13 @@ class VSRTaskWorker:
break
reporter = asyncio.create_task(_progress_reporter())
await loop.run_in_executor(None, _do)
reporter.cancel()
try:
await loop.run_in_executor(None, _do)
except Exception:
logger.exception(f"[vsr_worker] 任务 #{platform_task_id} 执行异常")
update_task(task_id, status="error", error="worker 执行异常,详见 Worker 日志")
finally:
reporter.cancel()
with task_lock:
final = tasks.get(task_id, {})