🤖 fix: prevent race condition when reloading with stale workspace #943
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.
Problem
On page reload in Electron, the app fails to restore the previously selected workspace and instead shows the home page with this error:
Root Cause
Two race conditions were causing this:
1. API connection timing
WorkspaceContext'sloadWorkspaceMetadata()returned early whenapiwas null (during initial 'connecting' state), butsetLoading(false)was still called. This causedAppto render with empty metadata whileselectedWorkspacewas restored from localStorage, triggering the validation effect to clear the selection.2. Missing metadata guard
Even after fixing the API timing, if
selectedWorkspacefrom localStorage referred to a deleted workspace, theAIViewwould try to render before the validation effect could clear the stale selection.Solution
Wait for API:
loadWorkspaceMetadatanow returns a boolean indicating success. The effect only callssetLoading(false)after actual load. Whenapibecomes available, the effect re-runs.Guard AIView render: If
selectedWorkspaceexists butcurrentMetadatais undefined, returnnullinstead of renderingAIView. The validation effect will clear the stale selection on the next tick.Remove redundant effect: Removed the simple validation effect (lines 126-130) since the comprehensive one (lines 165-189) handles all cases including missing fields update.
Generated with
mux