Skip to content

fix(web): resolve chat message loading race conditions and infinite loops#30695

Merged
JzoNgKVO merged 7 commits intolanggenius:mainfrom
MkDev11:fix/chat-log-race-conditions-30259
Jan 8, 2026
Merged

fix(web): resolve chat message loading race conditions and infinite loops#30695
JzoNgKVO merged 7 commits intolanggenius:mainfrom
MkDev11:fix/chat-log-race-conditions-30259

Conversation

@MkDev11
Copy link
Contributor

@MkDev11 MkDev11 commented Jan 7, 2026

Summary

Fix chat message loading race conditions, log disorder, and infinite loops in the app log panel.

Fixes #30259

Changes:

  • Add AbortController to cancel in-flight requests on new scroll
  • Add requestIdRef to ignore stale responses
  • Add oldestAnswerIdRef to store pagination anchor in ref (avoids stale closures)
  • Add lastLoadTimeRef for throttling that persists across effect re-runs
  • Add retryCountRef with MAX_RETRY_COUNT=3 to prevent infinite retry loops
  • Remove duplicate scroll listener that was firing both fetchData and loadMoreMessages
  • Use functional state updates in setAllChatItems to avoid stale state issues

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran make lint and make type-check (backend) and cd web && npx lint-staged (frontend) to appease the lint gods

Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=94194147

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jan 7, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @MkDev11, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses critical issues within the chat message loading mechanism in the app log panel, specifically targeting race conditions, log disorder, and infinite loops. By introducing robust request management, state synchronization, and retry logic, the changes significantly improve the stability and reliability of message loading and pagination, leading to a smoother user experience.

Highlights

  • Request Cancellation: Implemented AbortController to cancel in-flight requests during new scroll events, preventing race conditions.
  • Stale Response Handling: Added requestIdRef to ensure only the latest request's response is processed, effectively ignoring stale or out-of-order responses.
  • Pagination Anchor: Introduced oldestAnswerIdRef to store the pagination anchor, resolving stale closure issues and ensuring consistent message loading.
  • Throttling Persistence: Utilized lastLoadTimeRef for scroll throttling that maintains state across component re-renders, preventing excessive API calls.
  • Infinite Loop Prevention: Added retryCountRef with a MAX_RETRY_COUNT of 3 to prevent infinite retry loops that could occur during message loading under certain conditions.
  • Duplicate Scroll Listener Removal: Eliminated a redundant scroll listener that was causing fetchData and loadMoreMessages to fire simultaneously, simplifying logic and preventing unnecessary calls.
  • Stale State Resolution: Switched to functional state updates for setAllChatItems to avoid issues with stale state, ensuring data consistency when updating the chat message list.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the chat message loading and pagination logic within the DetailPanel component. Key changes include implementing request cancellation using AbortController and requestIdRef to prevent race conditions and ignore stale responses, and introducing oldestAnswerIdRef for a more robust pagination strategy to avoid stale closure issues. The loadMoreMessages function was enhanced with throttling using lastLoadTimeRef and a retry mechanism (retryCountRef, MAX_RETRY_COUNT) to improve scroll-based loading reliability, especially when no new unique messages are immediately available. Additionally, the scroll event listener was simplified, and state updates for allChatItems were converted to functional updates. The review comment points out a React anti-pattern where other state setters (setChatItemTree, setThreadChatItems) and ref updates (oldestAnswerIdRef) are performed inside the setAllChatItems functional updater. The reviewer suggests moving this logic into a separate useEffect hook that depends on allChatItems and hasMore to ensure the purity of the updater function and proper state management.

@MkDev11 MkDev11 force-pushed the fix/chat-log-race-conditions-30259 branch from 9de9b5e to b1918cd Compare January 7, 2026 10:00
…oops Fixes langgenius#30259 - Add AbortController to cancel in-flight requests on new scroll - Add requestIdRef to ignore stale responses - Add oldestAnswerIdRef to store pagination anchor in ref (avoids stale closures) - Add lastLoadTimeRef for throttling that persists across effect re-runs - Add retryCountRef with MAX_RETRY_COUNT=3 to prevent infinite retry loops - Remove duplicate scroll listener that was firing both fetchData and loadMoreMessages - Use functional state updates in setAllChatItems (pure updater functions) - Add useEffect to derive chatItemTree and threadChatItems from allChatItems - Remove unused SCROLL_THRESHOLD_PX constant - Remove allChatItems from useCallback dependency arrays
@MkDev11 MkDev11 force-pushed the fix/chat-log-race-conditions-30259 branch from b1918cd to 0e5b589 Compare January 7, 2026 10:25
@crazywoola
Copy link
Member

Please add a test for it as well :) The rest LGTM

Tests verify the core algorithms used to prevent race conditions: - Request deduplication (filtering duplicate message IDs) - Retry counter logic (MAX_RETRY_COUNT=3 limit) - Throttling logic (SCROLL_DEBOUNCE_MS=200ms) - AbortController cancellation of in-flight requests - Stale response detection via requestId comparison - Pagination anchor management (oldestAnswerIdRef) - Functional state update pattern for avoiding stale closures Related to langgenius#30259
@MkDev11
Copy link
Contributor Author

MkDev11 commented Jan 7, 2026

Please add a test for it as well :) The rest LGTM

Done!

@crazywoola
Copy link
Member

Please fix the lint errors.

Tests verify the core algorithms used to prevent race conditions: - Request deduplication (filtering duplicate message IDs) - Retry counter logic (MAX_RETRY_COUNT=3 limit) - Throttling logic (SCROLL_DEBOUNCE_MS=200ms) - AbortController cancellation of in-flight requests - Stale response detection via requestId comparison - Pagination anchor management (oldestAnswerIdRef) - Functional state update pattern for avoiding stale closures Related to langgenius#30259
@MkDev11 MkDev11 force-pushed the fix/chat-log-race-conditions-30259 branch from f35c150 to 1ad6cae Compare January 7, 2026 14:19
@MkDev11
Copy link
Contributor Author

MkDev11 commented Jan 8, 2026

@crazywoola Could you please merge the PR?

@crazywoola
Copy link
Member

We will review this later :)

@crazywoola crazywoola self-assigned this Jan 8, 2026
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jan 8, 2026
@JzoNgKVO JzoNgKVO merged commit 91d4471 into langgenius:main Jan 8, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

3 participants