[PR #25140] fix: prevent empty workflow draft sync during page navigation #30948

Closed
opened 2026-02-21 20:48:32 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/25140

State: closed
Merged: Yes


Summary

This PR fixes a race condition during page navigation that could cause empty workflow drafts to be synced to the server, overwriting valid data. The issue occurred when users switched pages while a debounced sync operation (5-second delay) was pending, causing the new component to mount with empty React Flow state and sync before data was loaded.

Root Cause

The system couldn't distinguish between "initializing with empty state" and "user intentionally cleared the workflow". The previous fix in commit 1d1bb9451e only checked for empty nodes with default viewport coordinates, which was unreliable and easily bypassed.

Solution

Introduced explicit data loading state management through:

  • Added isWorkflowDataLoaded flag to track when server data is fully loaded
  • Prevents sync operations until data loading is complete
  • Component lifecycle management to reset state and cancel pending operations on unmount
  • Replaced fragile viewport-based detection with reliable state-based checking

Key Changes

  • workflow-draft-slice.ts: Added isWorkflowDataLoaded state and setter method
  • use-workflow-init.ts: Set flag to true after successful data load from server
  • use-nodes-sync-draft.ts: Check flag before allowing any sync operations
  • workflow-app/index.tsx: Reset flag and cancel pending syncs on component unmount

Impact

  • Completely prevents empty draft sync during page navigation
  • Zero impact on normal workflow editing operations
  • Backward compatible - no changes to existing functionality
  • Pure defensive enhancement - adds safety without modifying business logic

Testing

This fix automatically protects against:

  • Fast page switching scenarios
  • Network delays during data loading
  • Component remounting situations
  • React Flow reinitialization cases

Normal operations remain unaffected:

  • Workflow editing and saving
  • Intentional workflow clearing by users
  • All existing user interactions

Screenshots

No UI changes - this is a backend logic fix for race condition handling.

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 `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/25140 **State:** closed **Merged:** Yes --- ## Summary This PR fixes a race condition during page navigation that could cause empty workflow drafts to be synced to the server, overwriting valid data. The issue occurred when users switched pages while a debounced sync operation (5-second delay) was pending, causing the new component to mount with empty React Flow state and sync before data was loaded. ## Root Cause The system couldn't distinguish between "initializing with empty state" and "user intentionally cleared the workflow". The previous fix in commit 1d1bb9451e only checked for empty nodes with default viewport coordinates, which was unreliable and easily bypassed. ## Solution Introduced explicit data loading state management through: - **Added `isWorkflowDataLoaded` flag** to track when server data is fully loaded - **Prevents sync operations** until data loading is complete - **Component lifecycle management** to reset state and cancel pending operations on unmount - **Replaced fragile viewport-based detection** with reliable state-based checking ## Key Changes - `workflow-draft-slice.ts`: Added `isWorkflowDataLoaded` state and setter method - `use-workflow-init.ts`: Set flag to true after successful data load from server - `use-nodes-sync-draft.ts`: Check flag before allowing any sync operations - `workflow-app/index.tsx`: Reset flag and cancel pending syncs on component unmount ## Impact - ✅ **Completely prevents** empty draft sync during page navigation - ✅ **Zero impact** on normal workflow editing operations - ✅ **Backward compatible** - no changes to existing functionality - ✅ **Pure defensive enhancement** - adds safety without modifying business logic ## Testing This fix automatically protects against: - Fast page switching scenarios - Network delays during data loading - Component remounting situations - React Flow reinitialization cases Normal operations remain unaffected: - Workflow editing and saving - Intentional workflow clearing by users - All existing user interactions ## Screenshots No UI changes - this is a backend logic fix for race condition handling. ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran \`dev/reformat\`(backend) and \`cd web && npx lint-staged\`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:48:32 -05:00
yindo closed this issue 2026-02-21 20:48:32 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#30948