Skip to content

Commit 0368400

Browse files
Fix race condition destroying terminals when opening terminals view
The cleanup effect in /terminals that removes orphaned worktree terminals was running before tasks finished loading. Since allTaskWorktrees derives from tasks (defaulting to []), all worktree terminals appeared orphaned and were destroyed. This was triggered by opening the terminals view in a new browser tab - terminals arrived via WebSocket faster than tasks via HTTP API. Added isTasksLoading guard to prevent cleanup until tasks are loaded.
1 parent 16c6235 commit 0368400

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/routes/terminals/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function TerminalsView() {
6060
}
6161
}, [tabs, activeTabId, isViewStateLoading, setActiveTab])
6262

63-
const { data: tasks = [] } = useTasks()
63+
const { data: tasks = [], isLoading: isTasksLoading } = useTasks()
6464
const { data: repositories = [] } = useRepositories()
6565
const { data: worktreeBasePath } = useWorktreeBasePath()
6666
const [repoFilter, setRepoFilter] = useState<string | null>(null)
@@ -136,7 +136,9 @@ function TerminalsView() {
136136

137137
// Destroy orphaned worktree terminals (terminals in worktrees dir but no matching task)
138138
useEffect(() => {
139-
if (!worktreeBasePath) return
139+
// Don't run cleanup until tasks are loaded - otherwise we'd destroy valid terminals
140+
// because allTaskWorktrees would be empty while tasks are still loading
141+
if (!worktreeBasePath || isTasksLoading) return
140142

141143
for (const terminal of terminals) {
142144
const isInWorktreesDir = terminal.cwd?.startsWith(worktreeBasePath)
@@ -146,7 +148,7 @@ function TerminalsView() {
146148
destroyTerminal(terminal.id)
147149
}
148150
}
149-
}, [terminals, allTaskWorktrees, worktreeBasePath, destroyTerminal])
151+
}, [terminals, allTaskWorktrees, worktreeBasePath, destroyTerminal, isTasksLoading])
150152

151153
// Filter terminals for the active tab
152154
const visibleTerminals = useMemo(() => {

0 commit comments

Comments
 (0)