Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

refactor: effect cleanup for timeouts (#1823)

Mike Fortman8f439463cc8a
diff --git a/frontend/app/auth/callback/page.tsx b/frontend/app/auth/callback/page.tsx
index 3ce4c82b4..62e8753e3 100644
--- a/frontend/app/auth/callback/page.tsx
+++ b/frontend/app/auth/callback/page.tsx
@@ -35,6 +35,9 @@ function AuthCallbackContent() {
}
sessionStorage.setItem(callbackKey, "true");
+ let cancelled = false;
+ let redirectTimeoutId: NodeJS.Timeout;
+
const handleCallback = async () => {
try {
// Get parameters from URL
@@ -158,9 +161,11 @@ function AuthCallbackContent() {
localStorage.removeItem("auth_redirect_to");
// Redirect to the original page or home
- setTimeout(() => {
- router.push(redirectTo);
- }, 2000);
+ if (!cancelled) {
+ redirectTimeoutId = setTimeout(() => {
+ if (!cancelled) router.push(redirectTo);
+ }, 2000);
+ }
} else {
// Connector authentication - redirect to connectors page
@@ -170,9 +175,11 @@ function AuthCallbackContent() {
localStorage.removeItem("auth_purpose");
// Redirect to settings page with success indicator
- setTimeout(() => {
- router.push("/settings?oauth_success=true");
- }, 2000);
+ if (!cancelled) {
+ redirectTimeoutId = setTimeout(() => {
+ if (!cancelled) router.push("/settings?oauth_success=true");
+ }, 2000);
+ }
}
} else {
throw new Error(result.error || "Authentication failed");
@@ -191,6 +198,10 @@ function AuthCallbackContent() {
};
handleCallback();
+ return () => {
+ cancelled = true;
+ clearTimeout(redirectTimeoutId);
+ };
}, [searchParams, router, refreshAuth]);
// Dynamic UI content based on purpose
diff --git a/frontend/app/chat/page.tsx b/frontend/app/chat/page.tsx
index dc89373cf..589835e75 100644
--- a/frontend/app/chat/page.tsx
+++ b/frontend/app/chat/page.tsx
@@ -296,6 +296,7 @@ function ChatPage() {
// Load conversation data from context
useEffect(() => {
+ let focusTimeoutId: NodeJS.Timeout;
// Only load conversation data when:
// 1. conversationData exists AND
// 2. (It's a different conversation OR we're not streaming and data has changed) AND
@@ -487,13 +488,15 @@ function ChatPage() {
}));
// Focus input when loading a conversation
- setTimeout(() => {
+ focusTimeoutId = setTimeout(() => {
chatInputRef.current?.focusInput();
}, 100);
} else if (!conversationData) {
// No conversation selected (new conversation)
lastLoadedConversationRef.current = null;
}
+
+ return () => clearTimeout(focusTimeoutId);
}, [
conversationData,
isUserInteracting,
@@ -505,16 +508,18 @@ function ChatPage() {
// Handle new conversation creation - only reset messages when placeholderConversation is set
useEffect(() => {
+ let focusTimeoutId: NodeJS.Timeout;
if (placeholderConversation && currentConversationId === null) {
console.log("Starting new conversation");
setMessages([INITIAL_ASSISTANT_MESSAGE]);
lastLoadedConversationRef.current = null;
// Focus input when starting a new conversation
- setTimeout(() => {
+ focusTimeoutId = setTimeout(() => {
chatInputRef.current?.focusInput();
}, 100);
}
+ return () => clearTimeout(focusTimeoutId);
}, [placeholderConversation, currentConversationId]);
const { isOnboardingComplete } = useOnboardingState();
diff --git a/frontend/app/onboarding/_components/onboarding-card.tsx b/frontend/app/onboarding/_components/onboarding-card.tsx
index 9fa4d10ec..0c97148e8 100644
--- a/frontend/app/onboarding/_components/onboarding-card.tsx
+++ b/frontend/app/onboarding/_components/onboarding-card.tsx
@@ -272,6 +272,7 @@ const OnboardingCard = ({
// Monitor tasks and call onComplete when all tasks are done
useEffect(() => {
+ let completeTimeoutId: NodeJS.Timeout;
if (currentStep === null || !tasks || !isEmbedding) {
return;
}
@@ -404,10 +405,12 @@ const OnboardingCard = ({
// Set to final step to show "Done"
setCurrentStep(totalSteps);
// Wait a bit before completing
- setTimeout(() => {
+ completeTimeoutId = setTimeout(() => {
onComplete();
}, 1000);
}
+
+ return () => clearTimeout(completeTimeoutId);
}, [
tasks,
currentStep,
diff --git a/frontend/app/onboarding/_components/onboarding-upload.tsx b/frontend/app/onboarding/_components/onboarding-upload.tsx
index bc91a47ff..b18e32fff 100644
--- a/frontend/app/onboarding/_components/onboarding-upload.tsx
+++ b/frontend/app/onboarding/_components/onboarding-upload.tsx
@@ -46,6 +46,8 @@ const OnboardingUpload = ({ onComplete }: OnboardingUploadProps) => {
// Monitor tasks and call onComplete when file processing is done
useEffect(() => {
+ let cancelled = false;
+ let completeTimeoutId: NodeJS.Timeout;
if (currentStep === null || !tasks || !uploadedTaskId) {
return;
}
@@ -161,21 +163,30 @@ const OnboardingUpload = ({ onComplete }: OnboardingUploadProps) => {
refetchNudges();
// Wait a bit before completing (after filter is created)
- setTimeout(() => {
- onComplete();
- }, 1000);
+ if (!cancelled) {
+ completeTimeoutId = setTimeout(() => {
+ if (!cancelled) onComplete();
+ }, 1000);
+ }
});
- } else {
+ } else if (!isCreatingFilter) {
// No filter to create, just complete
// Refetch nudges to get new ones
refetchNudges();
// Wait a bit before completing
- setTimeout(() => {
- onComplete();
- }, 1000);
+ if (!cancelled) {
+ completeTimeoutId = setTimeout(() => {
+ if (!cancelled) onComplete();
+ }, 1000);
+ }
}
}
+
+ return () => {
+ cancelled = true;
+ clearTimeout(completeTimeoutId);
+ };
}, [
tasks,
currentStep,
diff --git a/frontend/app/settings/_components/agent-settings-section.tsx b/frontend/app/settings/_components/agent-settings-section.tsx
index 8f319f458..8eb874aaf 100644
--- a/frontend/app/settings/_components/agent-settings-section.tsx
+++ b/frontend/app/settings/_components/agent-settings-section.tsx
@@ -194,7 +194,8 @@ export function AgentSettingsSection() {
const newParams = new URLSearchParams(searchParams.toString());
newParams.delete("focusLlmModel");
router.replace(`${pathname}?${newParams.toString()}`, { scroll: false });
- setTimeout(() => setOpenLlmSelector(false), 100);
+ const timeoutId = setTimeout(() => setOpenLlmSelector(false), 100);
+ return () => clearTimeout(timeoutId);
}
}, [focusLlmModel, searchParams, router, pathname]);