Skip to content

Fix race conditions in AI function call processing#8369

Merged
4ian merged 4 commits intoclaude/upgrade-react-18-6oNEDfrom
claude/fix-ask-ai-blocking-kiJjt
Mar 10, 2026
Merged

Fix race conditions in AI function call processing#8369
4ian merged 4 commits intoclaude/upgrade-react-18-6oNEDfrom
claude/fix-ask-ai-blocking-kiJjt

Conversation

@4ian
Copy link
Owner

@4ian 4ian commented Mar 10, 2026

Summary

This PR fixes race conditions in the AI generation system that could cause duplicate function call processing and stuck "sending" states. The changes add proper synchronization guards and improve error handling to prevent concurrent processing of the same function calls.

Key Changes

  • Added re-entrancy guard in useProcessFunctionCalls: Introduced a processingCallIdsRef to track function calls currently being processed, preventing the same call from being processed multiple times due to React 18's automatic batching behavior. The guard is cleaned up in a finally block to allow retries if needed.

  • Wrapped function call processing in try-finally: Ensures cleanup of the processing guard even if an error occurs during function call execution.

  • Improved send state management in AskAiEditor:

    • Separated the isSendingAiRequest check into its own conditional block with informative logging
    • Added console info messages for all early returns in the send effect to aid debugging
    • Fixed a bug where setSendingAiRequest(false) was not called in the error handler, which could leave the UI in a stuck "sending" state

Implementation Details

The race condition occurred because React 18's automatic batching defers state updates. When addEditorFunctionCallResults('working') was called, the state update was deferred, allowing the effect to re-fire before the 'working' state became visible. This caused the same function calls to be processed multiple times.

The solution uses a ref-based set to track call IDs currently being processed, which is synchronous and not affected by batching. The guard filters out any calls already in progress and cleans them up after processing completes (or fails).

The error handling fix ensures that if sending fails, the UI properly resets the sending state so users can retry.

https://claude.ai/code/session_019BRFfuhnwRNmHJyk429KWm

claude and others added 4 commits March 10, 2026 20:41
…ent back

Two fixes for a bug where the AI conversation gets stuck after processing
function calls (especially in orchestrator mode), directly related to the
React 18 upgrade:

1. Use a ref as source of truth for editor function call results
   (AiRequestContext.js): getEditorFunctionCallResults previously read from
   React state, which is subject to React 18 automatic batching. After
   calling addEditorFunctionCallResults('working'), the state update was
   deferred, so subsequent reads returned stale data without 'working'
   markers. Now uses a ref for synchronous reads with forceUpdate() for
   UI re-renders.

2. Add in-flight guard against duplicate function call processing
   (Utils.js): Defense-in-depth ref-based set that prevents concurrent
   processing of the same call_id, covering React 18 StrictMode
   double-invocations and any remaining re-entrancy edge cases.

In React 17, setState in async callbacks was synchronous, so 'working'
markers were visible immediately and prevented re-firing. React 18 batches
these updates, which could cause the processing effect to re-fire before
the markers were visible.

https://claude.ai/code/session_019BRFfuhnwRNmHJyk429KWm
Add console.info/error at key decision points where the send or processing
flow can silently bail out, to make future debugging easier:

- onSendMessage: log when blocked by isSendingAiRequest, hasUnfinishedResult,
  or hasFunctionsCallsToProcess guards
- onSendMessage catch: log the error before storing it
- onProcessFunctionCalls: log when skipped by in-flight guard or when
  results are discarded due to suspension/abort

https://claude.ai/code/session_019BRFfuhnwRNmHJyk429KWm
@4ian 4ian merged commit 02a50e4 into claude/upgrade-react-18-6oNED Mar 10, 2026
5 of 6 checks passed
@4ian 4ian deleted the claude/fix-ask-ai-blocking-kiJjt branch March 10, 2026 22:13
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.

2 participants