fix: add error handling to APISearchAgent to prevent hanging requests#1099
Open
octo-patch wants to merge 2 commits intoItzCrazyKns:masterfrom
Open
fix: add error handling to APISearchAgent to prevent hanging requests#1099octo-patch wants to merge 2 commits intoItzCrazyKns:masterfrom
octo-patch wants to merge 2 commits intoItzCrazyKns:masterfrom
Conversation
added 2 commits
April 8, 2026 10:25
…yKns#1006) - Wrap SearchAgent.searchAsync in try/catch: on failure, emit an error event to the session and update the message status to 'error' in the database. Previously, any exception left the message stuck in 'answering' state forever. - In useChat.tsx checkReconnect: check res.ok before reading the stream. When the backend session no longer exists (e.g. after a server restart), the reconnect API returns 404. The frontend now marks the message as 'error' and exits loading state instead of hanging indefinitely.
When an error occurs during search, APISearchAgent.searchAsync had no try-catch, causing the /api/search route Promise to never resolve since no error event was emitted to the session. Fixes ItzCrazyKns#1080
Contributor
There was a problem hiding this comment.
1 issue found across 3 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/lib/hooks/useChat.tsx">
<violation number="1" location="src/lib/hooks/useChat.tsx:428">
P2: Reconnect flow lacks top-level fetch failure handling, which can leave reconnect/loading state stuck and reject unhandled.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| method: 'POST', | ||
| }); | ||
|
|
||
| if (!res.ok) { |
Contributor
There was a problem hiding this comment.
P2: Reconnect flow lacks top-level fetch failure handling, which can leave reconnect/loading state stuck and reject unhandled.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/lib/hooks/useChat.tsx, line 428:
<comment>Reconnect flow lacks top-level fetch failure handling, which can leave reconnect/loading state stuck and reject unhandled.</comment>
<file context>
@@ -425,6 +425,20 @@ export const ChatProvider = ({ children }: { children: React.ReactNode }) => {
method: 'POST',
});
+ if (!res.ok) {
+ // Session no longer exists (e.g. server restarted) - mark message as error
+ setLoading(false);
</file context>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1080
Problem
APISearchAgent.searchAsynchad no try-catch block. When any error occurs during search (e.g., model provider failures, classification errors), noendorerrorevent is emitted to the session. The/api/searchroute creates a Promise that waits for these events — when none arrive, the API request hangs indefinitely, returning no response to the client.This results in unhandled rejections logged as:
Solution
Wrap
searchAsyncin try-catch and emit anerrorevent on failure, matching the existing pattern inSearchAgent(the chat UI agent) which already handles errors this way.Testing
/api/searchendpointSummary by cubic
Add error handling to both search agents and the reconnect flow to prevent hanging requests and stuck messages. Errors now emit proper events, update message status to
error, and return a 500 instead of hanging.APISearchAgent.searchAsyncin try/catch and emit a sessionerrorto prevent/api/searchfrom hanging.SearchAgent.searchAsyncin try/catch; emiterrorand set the message’s DB status toerror.useChatreconnect, checkres.ok; on missing session (e.g., 404), stop loading and mark the last message aserror.Written for commit 6ccc07f. Summary will update on new commits.