Skip to content

Fix git clone race condition in Clone Repository dialog#4

Open
lucky7323 wants to merge 1 commit intoCortexLM:mainfrom
lucky7323:fix/clone-race-condition-21724
Open

Fix git clone race condition in Clone Repository dialog#4
lucky7323 wants to merge 1 commit intoCortexLM:mainfrom
lucky7323:fix/clone-race-condition-21724

Conversation

@lucky7323
Copy link

Problem

The Clone Repository functionality suffered from a race condition where git clone commands were silently ignored when terminals took longer than 500ms to initialize. This led to users believing their clone operations succeeded when they actually failed.

Root Cause

The original implementation used an arbitrary 500ms setTimeout() before sending the git clone command:

window.dispatchEvent(new CustomEvent("terminal:new"));
await new Promise(r => setTimeout(r, 500)); // ❌ Race condition
window.dispatchEvent(new CustomEvent("terminal:write-active", {
  detail: { data: `git clone "${url}" "${targetDir}"\n` }
}));

If the terminal wasn't ready within exactly 500ms, the terminal:write-active event would be sent to a non-existent or inactive terminal, causing the command to be silently ignored.

Solution

Replace the arbitrary timeout with proper event-driven terminal ready detection:

  • Event-driven approach: Listen for the terminal:created event from the backend
  • Generous timeout: 10-second maximum wait time (vs. 500ms)
  • Fallback mechanism: Secondary 1-second fallback if events are missed
  • Shell escaping: Add proper argument escaping to prevent command injection
  • Error handling: Proper error notifications when terminal creation fails

Technical Details

  • Uses the existing terminal:created Tauri event that's already handled in TerminalsContext
  • Implements proper shell argument escaping with single quotes
  • Provides clear error messages to users when operations fail
  • Maintains backward compatibility with existing terminal system

Testing

  • Verified terminal:created event exists and is handled
  • Confirmed shell escaping prevents injection attacks
  • Added comprehensive error handling and user notifications
  • Maintained compatibility with existing terminal workflow

This fix ensures git clone operations are reliable and provides proper feedback to users when issues occur.

Fixes #21724

- Replace unreliable 500ms timeout with proper terminal:created event
- Add shell argument escaping to prevent command injection
- Implement 10-second timeout with fallback for terminal creation
- Improve error handling with user-facing notifications

Fixes race condition where git clone commands were silently ignored
when terminals took longer than 500ms to initialize.

Addresses issue #21724
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant