Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add timeout to clipboard initialization #185

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mod util;

use std::{
ops::Deref,
sync::mpsc,
thread,
time::{Duration, Instant},
};

Expand Down Expand Up @@ -283,11 +285,31 @@ async fn main() -> eyre::Result<()> {
.try_init()?;
}

let mut clipboard = ClipboardContext::new()
.map_err(|err| {
log::warn!("Failed to initialize clipboard support: {}", err);
})
.ok();
// cli_clipboard can hang if unable to connect to x11 socket in linux
// so we try for up to 1 second
let (sender, receiver) = mpsc::channel();
let _clipboard_init_thread = thread::spawn(move || {
match sender.send(
ClipboardContext::new()
.map_err(|err| {
log::warn!("Failed to initialize clipboard support: {}", err);
})
.ok(),
) {
Ok(()) => {}, // everything good
Err(_) => {}, // we have been released, don't panic
}
});
let mut clipboard = match receiver.recv_timeout(Duration::from_millis(1000)) {
Copy link
Contributor Author

@jonZlotnik jonZlotnik Mar 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 second is probably over-cautious, if anybody can defend a shorter timeout, pls do. (I didn't do any research into how long it takes for cli_clipboard to init normally)

Ok(initialized_clipboard) => {
log::debug!("Clipboard initialized successfully");
initialized_clipboard
},
Err(err) => {
log::warn!("Clipboard initialization timed out: {}", err);
None
},
};

let concat_file_name = |file_path: &Path, file_name: Option<_>| {
// TODO this has gotten out of hand (it ugly)
Expand Down