fix: prevent stale features from race condition in AISettings useEffect

Reset to fallbackFeatures when fetch condition is not met, and use a
cancelled flag to ignore responses from outdated requests after unmount
or dependency change.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
jackkav
2026-05-26 11:01:25 +02:00
committed by Jack Kavanagh
parent 977677cd2d
commit cd9ddb07ed

View File

@@ -20,11 +20,15 @@ export const AISettings = () => {
const [features, setFeatures] = useState<FeatureList>(fallbackFeatures);
useEffect(() => {
if (organizationId && userSession.id && !models.organization.isScratchpadOrganizationId(organizationId)) {
getOrganizationFeatures({ organizationId, sessionId: userSession.id })
.then(res => setFeatures(res?.features || fallbackFeatures))
.catch(() => setFeatures(fallbackFeatures));
if (!organizationId || !userSession.id || models.organization.isScratchpadOrganizationId(organizationId)) {
setFeatures(fallbackFeatures);
return;
}
let cancelled = false;
getOrganizationFeatures({ organizationId, sessionId: userSession.id })
.then(res => { if (!cancelled) { setFeatures(res?.features || fallbackFeatures); } })
.catch(() => { if (!cancelled) { setFeatures(fallbackFeatures); } });
return () => { cancelled = true; };
}, [organizationId, userSession.id]);
const [currentLLM, setCurrentLLM] = useState<LLMConfig | null>(null);
const [selectedBackend, setSelectedBackend] = useState<LLMBackend>('gguf');