fix: linux, arboard::new(), retry (#8538)

Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
fufesou 2024-06-30 01:13:45 +08:00 committed by GitHub
parent bd334769fa
commit 1f4c62e480
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1524,18 +1524,23 @@ impl ClipboardContext {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub fn new() -> ResultType<ClipboardContext> { pub fn new() -> ResultType<ClipboardContext> {
for _ in 0..3 { let mut i = 1;
let (tx, rx) = std::sync::mpsc::channel(); loop {
std::thread::spawn(move || { // Try 5 times to create clipboard
tx.send(arboard::Clipboard::new()).ok(); // Arboard::new() connect to X server or Wayland compositor, which shoud be ok at most time
}); // But sometimes, the connection may fail, so we retry here.
match rx.recv_timeout(Duration::from_millis(40)) { match arboard::Clipboard::new() {
Ok(Ok(c)) => return Ok(ClipboardContext(c)), Ok(x) => return Ok(ClipboardContext(x)),
Ok(Err(e)) => return Err(e.into()), Err(e) => {
Err(_) => continue, if i == 5 {
return Err(e.into());
} else {
std::thread::sleep(std::time::Duration::from_millis(30 * i));
}
}
} }
i += 1;
} }
bail!("Failed to create clipboard context, timeout");
} }
#[inline] #[inline]
@ -1544,11 +1549,6 @@ impl ClipboardContext {
Ok(self.0.get_text()?) Ok(self.0.get_text()?)
} }
// CAUTION: This function must not be called in the main thread!!! It can only be called in the clibpoard thread.
// There's no timeout for this function, so it may block.
// Because of https://github.com/1Password/arboard/blob/151e679ee5c208403b06ba02d28f92c5891f7867/src/platform/linux/x11.rs#L296
// We cannot use a new thread to get text because the clipboard context is `&mut self`.
// The crate design is somehow not good.
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub fn get_text(&mut self) -> ResultType<String> { pub fn get_text(&mut self) -> ResultType<String> {
Ok(self.0.get_text()?) Ok(self.0.get_text()?)