diff --git a/web/src/app/(main)/function/tasks/detail/_tabs/overview-tab.tsx b/web/src/app/(main)/function/tasks/detail/_tabs/overview-tab.tsx index dbf9b133..8bde8442 100644 --- a/web/src/app/(main)/function/tasks/detail/_tabs/overview-tab.tsx +++ b/web/src/app/(main)/function/tasks/detail/_tabs/overview-tab.tsx @@ -1137,37 +1137,42 @@ function TaskInterceptRulesCard({ taskId }: { taskId: string }) { const [editPattern, setEditPattern] = React.useState(""); const [editNote, setEditNote] = React.useState(""); // Switching between two task details keeps this component mounted, so a response can arrive after the - // operator moved to another task. Every read and write carries the request it belongs to, and a response - // that is no longer the latest one is dropped instead of painting another task's state. + // operator moved to another task. Two things guard every state write: taskIdRef names the task the card + // shows now (a write started for the previous task returns early), and the request counters drop a + // response that a newer request for the same task has already superseded. + const taskIdRef = React.useRef(taskId); + taskIdRef.current = taskId; const rulesRequestRef = React.useRef(0); const gateRequestRef = React.useRef(0); // The rules and the gate are read separately: a failure in one must not hide the other, and a failed // read must never leave the switch disabled with no way to retry. const loadRules = React.useCallback(async () => { + if (taskIdRef.current !== taskId) return; const request = ++rulesRequestRef.current; try { const list = await api.taskInterceptRules(taskId); - if (rulesRequestRef.current !== request) return; + if (taskIdRef.current !== taskId || rulesRequestRef.current !== request) return; setRules(list); setErr(""); } catch (e) { - if (rulesRequestRef.current !== request) return; + if (taskIdRef.current !== taskId || rulesRequestRef.current !== request) return; setErr((e as Error).message); } }, [taskId]); const loadGate = React.useCallback(async () => { + if (taskIdRef.current !== taskId) return; const request = ++gateRequestRef.current; setGateErr(""); try { const task = await api.task(taskId); - if (gateRequestRef.current !== request) return; + if (taskIdRef.current !== taskId || gateRequestRef.current !== request) return; // A backend that omits the field reads as unknown, never as "off". setTargetGate(typeof task.target_gate === "boolean" ? task.target_gate : undefined); setTaskFinished(task.status === "done" || task.status === "failed" || task.status === "timeout"); } catch (e) { - if (gateRequestRef.current !== request) return; + if (taskIdRef.current !== taskId || gateRequestRef.current !== request) return; setTargetGate(undefined); setGateErr((e as Error).message); } @@ -1194,13 +1199,13 @@ function TaskInterceptRulesCard({ taskId }: { taskId: string }) { setGateErr(""); try { const task = await api.setTaskTargetGate(taskId, next); - if (gateRequestRef.current !== request) return; + if (taskIdRef.current !== taskId || gateRequestRef.current !== request) return; setTargetGate(typeof task.target_gate === "boolean" ? task.target_gate : undefined); } catch (e) { - if (gateRequestRef.current !== request) return; + if (taskIdRef.current !== taskId || gateRequestRef.current !== request) return; setGateErr((e as Error).message); } finally { - if (gateRequestRef.current === request) setGateBusy(false); + if (taskIdRef.current === taskId && gateRequestRef.current === request) setGateBusy(false); } } @@ -1216,13 +1221,15 @@ function TaskInterceptRulesCard({ taskId }: { taskId: string }) { note: newNote.trim(), enabled: true, }); + if (taskIdRef.current !== taskId) return; setNewPattern(""); setNewNote(""); await loadRules(); } catch (e) { + if (taskIdRef.current !== taskId) return; setErr((e as Error).message); } finally { - setBusy(false); + if (taskIdRef.current === taskId) setBusy(false); } } @@ -1247,12 +1254,14 @@ function TaskInterceptRulesCard({ taskId }: { taskId: string }) { note: editNote.trim(), enabled: r.enabled, }); + if (taskIdRef.current !== taskId) return; setEditId(null); await loadRules(); } catch (e) { + if (taskIdRef.current !== taskId) return; setErr((e as Error).message); } finally { - setBusy(false); + if (taskIdRef.current === taskId) setBusy(false); } }