fix: 设备异常状态清除(清除异常/一键清除,检查与更新原子化防竞态)

This commit is contained in:
2026-08-11 08:22:24 +08:00
parent 5f914c4fe4
commit 8a3cd67a97
4 changed files with 86 additions and 10 deletions
+30
View File
@@ -115,6 +115,36 @@ def _remove_worker(serial):
_WORKERS.pop(serial, None)
def clear_worker_error(serial):
"""清除设备错误/失败状态,恢复为 idle。运行中/连接中返回 False 不清理。
检查与更新在同一把锁内完成,避免与任务启动写 connecting/running 的竞态。
"""
with _WORKERS_LOCK:
w = _WORKERS.get(serial)
if w is None:
return True # 本就没有状态记录,视作已清理
if w.get("status") in ("running", "connecting"):
return False
w.update(status="idle", last_error="", last_warning="", task_job="",
current_action="", attempt=0, progress={}, end_time=0,
last_heartbeat=time.time())
return True
def clear_all_worker_errors(exclude=()):
"""批量清除所有 error/failed 状态(exclude 里的跳过)。返回清除数。"""
exclude = set(exclude)
with _WORKERS_LOCK:
targets = [s for s, w in _WORKERS.items()
if s not in exclude and w.get("status") in ("error", "failed")]
cleared = 0
for s in targets:
if clear_worker_error(s):
cleared += 1
return cleared
def get_worker_heartbeat(serial):
"""返回 worker 最近一次心跳时间,没有返回 0。"""
with _WORKERS_LOCK: