Skip to content

Commit d38381c

Browse files
committed
Fix race condition in archived threads useEffect
Add a request ID ref to refreshArchivedThreads so that when multiple concurrent fetches race (e.g. due to rapid environmentIds changes), only the latest invocation's result is applied to state. Stale responses are silently discarded.
1 parent 2f3b92c commit d38381c

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

apps/web/src/components/settings/SettingsPanels.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,10 @@ export function ArchivedThreadsPanel() {
13101310
[projects],
13111311
);
13121312

1313+
const refreshIdRef = useRef(0);
1314+
13131315
const refreshArchivedThreads = useCallback(async () => {
1316+
const id = ++refreshIdRef.current;
13141317
setIsLoadingArchive(true);
13151318
try {
13161319
const snapshots = await Promise.all(
@@ -1325,9 +1328,13 @@ export function ArchivedThreadsPanel() {
13251328
};
13261329
}),
13271330
);
1328-
setArchivedSnapshots(snapshots.filter((snapshot) => snapshot !== null));
1331+
if (id === refreshIdRef.current) {
1332+
setArchivedSnapshots(snapshots.filter((snapshot) => snapshot !== null));
1333+
}
13291334
} finally {
1330-
setIsLoadingArchive(false);
1335+
if (id === refreshIdRef.current) {
1336+
setIsLoadingArchive(false);
1337+
}
13311338
}
13321339
}, [environmentIds]);
13331340

0 commit comments

Comments
 (0)