Skip to content

Multiple webview panels can be opened for the same worktree causing race conditions #15

@SunYanbox

Description

@SunYanbox

Description

openWorktreeWebview() in src/worktreeWebview.ts creates a new webview panel on every invocation (line 450-455) with no deduplication:

const panel = vscode.window.createWebviewPanel(
  'quickPrWorktreeDetail',
  `Worktree: ${info.branchName}`,
  { viewColumn: vscode.ViewColumn.Active, preserveFocus: true },
  { enableScripts: true, localResourceRoots: [], retainContextWhenHidden: true },
);

This leads to:

  1. Duplicate tabs — clicking "Open" on the same worktree multiple times creates multiple identical panels
  2. Stale data in old panels — if the user commits in panel A, panel B still shows old data with no notification
  3. Race conditions — simultaneous commits from two panels for the same worktree:
    • Files may be copied twice
    • Commit messages may overwrite each other
    • worktrees.json may experience concurrent write conflicts (though Node.js single-threaded nature mitigates this somewhat)

Affected Code

  • src/worktreeWebview.ts:450-455 — unconditional panel creation
  • src/worktreeManager.tsupdateWorktree / addWorktree have no locking

Suggested Solution

  1. Panel reuse — maintain a Map<string, vscode.WebviewPanel> keyed by worktreeId. If a panel already exists for this worktree, call panel.reveal() instead of creating a new one
  2. Clean up on dispose — remove the mapping when panel.onDidDispose fires
  3. Always refresh on reveal — before revealing an existing panel, re-fetch worktree data and rebuild the HTML
  4. (Optional) Operation lock — add a simple mutex per worktree ID to prevent concurrent commit/finalize/delete operations

Priority

Low. Real-world users rarely open multiple panels for the same worktree, but when it happens the bugs are hard to diagnose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions