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

Conversation

jonZlotnik
Copy link
Contributor

TL;DR

  • cli_clipboard hangs on initialization in some weird x11 setups (e.g. my WSL2)
  • In linux, cli_clipboard depends on a connection to a socket.
  • the clipboard functionality isn't critical, so we shouldn't fail if not working

Context:
I wanted to try my hand at the sending text feature,
so I tried getting this repo setup on WSL2, but I couldn't get my binary to run. It kept hanging with no output.

Into debug mode I went:
All I got was [2023-03-06T23:58:31Z DEBUG wormhole_rs] Logging enabled.

Now to debug Rust for the first time:
I found that it was stuck inside the cli_clipboard ClipboardContext::new() function.
And noticed that for Linux environments, it depends on an x11 socket.

image

There doesn't seem to be any kind of test suite for the cli at the moment, so let me know if there something I should add.

Note: I'm brand spanking new to Rust. So please be verbose with comments and teachings. 😅

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)

@jonZlotnik jonZlotnik marked this pull request as ready for review March 7, 2023 02:18
@codecov
Copy link

codecov bot commented Mar 7, 2023

Codecov Report

Patch coverage has no change and project coverage change: -0.17 ⚠️

Comparison is base (a82a7ae) 43.27% compared to head (79ebb44) 43.10%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #185      +/-   ##
==========================================
- Coverage   43.27%   43.10%   -0.17%     
==========================================
  Files          18       18              
  Lines        2542     2552      +10     
==========================================
  Hits         1100     1100              
- Misses       1442     1452      +10     
Impacted Files Coverage Δ
cli/src/main.rs 0.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@jonZlotnik jonZlotnik force-pushed the fix-hanging-clipboard branch from 4861ff2 to 554bbf8 Compare March 7, 2023 03:53
@jonZlotnik jonZlotnik force-pushed the fix-hanging-clipboard branch from 554bbf8 to 79ebb44 Compare March 7, 2023 15:19
@jonZlotnik
Copy link
Contributor Author

Note: I'm actually having trouble replicating the issue I faced in other environments (even other WSL2 envs).
Nonetheless, blocking the main thread of any program on waiting for an "networky" OS resource without a timeout is usually not a good idea.

@piegamesde
Copy link
Member

I'll try to have a look at it in the next couple of days. In the mean time, would you mind reporting the bug upstream to the relevant crate? Maybe they have some deadlock or other bug that can be fixed for that platform.

@jonZlotnik
Copy link
Contributor Author

jonZlotnik commented Mar 8, 2023

Issues seem to already be stacking up. Maybe switching to 1Password's arboard https://github.com/1Password/arboard could be good if they're not resolved timely?

cli_clipboard
allie-wake-up/cli-clipboard#16
allie-wake-up/cli-clipboard#17

x11_clipboard
quininer/x11-clipboard#32

@piegamesde you also added this and it's still open
allie-wake-up/cli-clipboard#10

@piegamesde
Copy link
Member

Thanks for digging into this. I'm fine if you want to make a switch to arboard, the only requirement I have is that it has native Wayland support. Using it would have the added benefit of not depending on any native X libraries, as we currently do.

@piegamesde
Copy link
Member

Finally had a look at the code. It looks okay, but I think it can be simplified: since we always only send one value, you can simply have it as return value of the closure and not use a channel at all. On the main thread, one would then join the clipboard thread with a timeout. If it succeeds print the value, otherwise kill the thread and print an error message.

Of course you could also try out arboard, which will probably make the background thread obsolete.

@jonZlotnik
Copy link
Contributor Author

jonZlotnik commented Mar 12, 2023

Okay, so I think you're correct in that the timeout fix should be upstream, but the cli_clipoard is too far downstream (depends on x11_clipboard, which depends on x11rb).

So I tried switching to arboard, but it actually has the same issue. Thankfully, they depend on x11rb directly, and a small fix is possible. So I submitted a PR there, and we'll see how it progresses.

1Password/arboard#100

If it goes through, I'll PR the switch to arboard.

@jonZlotnik
Copy link
Contributor Author

Finally had a look at the code. It looks okay, but I think it can be simplified: since we always only send one value, you can simply have it as return value of the closure and not use a channel at all. On the main thread, one would then join the clipboard thread with a timeout. If it succeeds print the value, otherwise kill the thread and print an error message.

Of course you could also try out arboard, which will probably make the background thread obsolete.

I actually don't think it's possible without a channel unless there's some API I missed for joining threads. (very well might be)
Don't want to wait the full time if the thread returns successfully.

@jonZlotnik
Copy link
Contributor Author

Thanks for digging into this. I'm fine if you want to make a switch to arboard, the only requirement I have is that it has native Wayland support. Using it would have the added benefit of not depending on any native X libraries, as we currently do.

Alright, so my PR finally got merged in 1Password/arboard#100

There seems to be the option to have Wayland by default:
https://github.com/1Password/arboard/blob/3399a2582f33085f9602b1acfaaa5847face8373/README.md?plain=1#L21-L27

@piegamesde
Copy link
Member

Okay, how to proceed with this? Do you want to try switching to arboard?

@jonZlotnik
Copy link
Contributor Author

Okay, how to proceed with this? Do you want to try switching to arboard?

Yeah I'll PR the switch to arboard. Sorry for the lapse.

@jonZlotnik
Copy link
Contributor Author

Switch is PR'd in #203

@jonZlotnik jonZlotnik closed this Jul 10, 2023
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.

2 participants