Commit 5bc5ae3
authored
🤖 fix: prevent race condition when reloading with stale workspace (#943)
## Problem
On page reload in Electron, the app fails to restore the previously
selected workspace and instead shows the home page with this error:
```
Workspace 6164ccb357 no longer exists, clearing selection
```
## Root Cause
Two race conditions were causing this:
### 1. API connection timing
`WorkspaceContext`'s `loadWorkspaceMetadata()` returned early when `api`
was null (during initial 'connecting' state), but `setLoading(false)`
was still called. This caused `App` to render with empty metadata while
`selectedWorkspace` was restored from localStorage, triggering the
validation effect to clear the selection.
### 2. Missing metadata guard
Even after fixing the API timing, if `selectedWorkspace` from
localStorage referred to a deleted workspace, the `AIView` would try to
render before the validation effect could clear the stale selection.
## Solution
1. **Wait for API**: `loadWorkspaceMetadata` now returns a boolean
indicating success. The effect only calls `setLoading(false)` after
actual load. When `api` becomes available, the effect re-runs.
2. **Guard AIView render**: If `selectedWorkspace` exists but
`currentMetadata` is undefined, return `null` instead of rendering
`AIView`. The validation effect will clear the stale selection on the
next tick.
3. **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`_1 parent b18381d commit 5bc5ae3
2 files changed
+21
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
121 | 121 | | |
122 | 122 | | |
123 | 123 | | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | 124 | | |
133 | 125 | | |
134 | 126 | | |
| |||
571 | 563 | | |
572 | 564 | | |
573 | 565 | | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
574 | 573 | | |
575 | 574 | | |
576 | 575 | | |
577 | | - | |
| 576 | + | |
578 | 577 | | |
579 | 578 | | |
580 | 579 | | |
581 | 580 | | |
582 | | - | |
| 581 | + | |
583 | 582 | | |
584 | 583 | | |
585 | 584 | | |
| |||
591 | 590 | | |
592 | 591 | | |
593 | 592 | | |
594 | | - | |
595 | | - | |
596 | | - | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
597 | 596 | | |
598 | 597 | | |
599 | 598 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
| 121 | + | |
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
| 131 | + | |
131 | 132 | | |
132 | 133 | | |
133 | 134 | | |
| 135 | + | |
134 | 136 | | |
135 | 137 | | |
136 | 138 | | |
137 | | - | |
| 139 | + | |
138 | 140 | | |
139 | 141 | | |
140 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
141 | 147 | | |
142 | 148 | | |
143 | 149 | | |
| |||
0 commit comments