fix(tui): prevent clipboard copy from blocking async event loop on Linux#123
Merged
fix(tui): prevent clipboard copy from blocking async event loop on Linux#123
Conversation
The safe_clipboard_copy() function was using arboard's blocking .wait() call on Linux which could hang indefinitely when no clipboard manager is available (e.g., SSH sessions without X11/Wayland forwarding). This caused the TUI to freeze when pressing 'c' on the login screen. Fix: Spawn the blocking clipboard operation in a separate thread with a 2-second timeout. If the operation times out or fails, return false gracefully with a warning log message. This ensures: - Clipboard copy works correctly on systems with clipboard support - Clipboard copy fails gracefully (returns false) on systems without clipboard support, without blocking the UI - The async event loop remains responsive
Greptile OverviewGreptile SummaryFixed a critical blocking issue on Linux where pressing 'c' during the login screen would freeze the TUI indefinitely when no clipboard manager was available. The solution spawns clipboard operations in a separate thread with a 2-second timeout, ensuring the async event loop remains responsive while preserving correct clipboard behavior on systems with clipboard support. Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| src/cortex-tui/src/runner/terminal.rs | Refactored Linux clipboard copy to use threaded timeout, preventing UI freezes when clipboard manager is unavailable |
Sequence Diagram
sequenceDiagram
participant User
participant LoginScreen
participant safe_clipboard_copy
participant Thread
participant Clipboard
participant ClipboardManager
User->>LoginScreen: Press 'c' key
LoginScreen->>safe_clipboard_copy: safe_clipboard_copy(url)
alt Linux
safe_clipboard_copy->>Thread: spawn thread
safe_clipboard_copy->>safe_clipboard_copy: create mpsc channel
Thread->>Clipboard: Clipboard::new()
alt Clipboard available
Clipboard-->>Thread: Ok(clipboard)
Thread->>ClipboardManager: clipboard.set().wait().text()
alt Manager receives data
ClipboardManager-->>Thread: Ok()
Thread->>safe_clipboard_copy: send(true)
else Manager error
ClipboardManager-->>Thread: Err(e)
Thread->>Thread: warn log
Thread->>safe_clipboard_copy: send(false)
end
else Clipboard unavailable
Clipboard-->>Thread: Err(e)
Thread->>Thread: debug log
Thread->>safe_clipboard_copy: send(false)
end
safe_clipboard_copy->>safe_clipboard_copy: recv_timeout(2s)
alt Received within timeout
safe_clipboard_copy-->>LoginScreen: result (true/false)
else Timeout
safe_clipboard_copy->>safe_clipboard_copy: warn log
safe_clipboard_copy-->>LoginScreen: false
else Disconnected
safe_clipboard_copy->>safe_clipboard_copy: warn log
safe_clipboard_copy-->>LoginScreen: false
end
else Windows/Other
safe_clipboard_copy->>Clipboard: Clipboard::new()
Clipboard-->>safe_clipboard_copy: Ok/Err
safe_clipboard_copy->>Clipboard: set_text()
Clipboard-->>safe_clipboard_copy: Ok/Err
safe_clipboard_copy-->>LoginScreen: result
end
alt Success
LoginScreen->>LoginScreen: Show "Copied!" notification
else Failure
LoginScreen->>LoginScreen: No notification
end
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Pressing the 'c' key on the Welcome screen (LoginScreen) while waiting for browser authentication caused the TUI to freeze/block completely, making the application unresponsive.
Root Cause
The
safe_clipboard_copy()function incortex-tui/src/runner/terminal.rsuses arboard's blocking.wait()call on Linux to ensure the clipboard manager receives the data. However, this call can hang indefinitely when no clipboard manager is available (e.g., SSH sessions without X11/Wayland forwarding).Solution
Spawn the blocking clipboard operation in a separate thread with a 2-second timeout. If the operation times out or fails, return
falsegracefully with a warning log message.This ensures:
false) on systems without clipboard support, without blocking the UICopied!notification only shows on success)Testing
cargo check -p cortex-tui