Skip to content

fix: add error handling to APISearchAgent to prevent hanging requests#1099

Open
octo-patch wants to merge 2 commits intoItzCrazyKns:masterfrom
octo-patch:fix/issue-1080-api-error-handling
Open

fix: add error handling to APISearchAgent to prevent hanging requests#1099
octo-patch wants to merge 2 commits intoItzCrazyKns:masterfrom
octo-patch:fix/issue-1080-api-error-handling

Conversation

@octo-patch
Copy link
Copy Markdown

@octo-patch octo-patch commented Apr 10, 2026

Fixes #1080

Problem

APISearchAgent.searchAsync had no try-catch block. When any error occurs during search (e.g., model provider failures, classification errors), no end or error event is emitted to the session. The /api/search route 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:

⨯ unhandledRejection: Error: <message>

Solution

Wrap searchAsync in try-catch and emit an error event on failure, matching the existing pattern in SearchAgent (the chat UI agent) which already handles errors this way.

Testing

  • Trigger an error in the search pipeline (e.g., use a misconfigured provider) via the /api/search endpoint
  • Before fix: request hangs with no response
  • After fix: request returns a 500 error response with the error message

Summary 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.

  • Bug Fixes
    • Wrap APISearchAgent.searchAsync in try/catch and emit a session error to prevent /api/search from hanging.
    • Wrap SearchAgent.searchAsync in try/catch; emit error and set the message’s DB status to error.
    • In useChat reconnect, check res.ok; on missing session (e.g., 404), stop loading and mark the last message as error.

Written for commit 6ccc07f. Summary will update on new commits.

octo-patch 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
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/lib/hooks/useChat.tsx
method: 'POST',
});

if (!res.ok) {
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with Cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cant use perplexica API (Error: is empty)

1 participant