YaLTeR / wl-clipboard-rs

A safe Rust crate for working with the Wayland clipboard.
Apache License 2.0
223 stars 16 forks source link

paste::get_contents leaks file descriptors to unix pipes #12

Open e00E opened 4 years ago

e00E commented 4 years ago
sway version 1.4
[package]
name = "clipboard"
version = "0.1.0"
edition = "2018"

[dependencies]
wl-clipboard-rs = "0.4"
use wl_clipboard_rs::paste::{get_contents, ClipboardType, MimeType, Seat};

fn main() {
    let mut i: u64 = 0;
    loop {
        println!("iteration {}", i);
        // Sometimes the program hangs here forever after exhausting file descriptors.
        let result = get_contents(ClipboardType::Regular, Seat::Unspecified, MimeType::Text);
        if let Err(err) = result {
            println!("first error: {}", err);
            // Sleep so that we have time to inspect number of file descriptors.
            std::thread::sleep(std::time::Duration::from_secs(10));
            return;
        }
        i += 1;
    }
}
cargo build && target/debug/clipboard

On my machine I usually see output like:

...
iteration 454
[wayland-client] Protocol error 1 on object wl_display@1: invalid arguments for zwlr_data_control_offer_v1@4278190080.receive
first error: Wayland compositor communication error

When this happens I check the open file descriptors (the binary is called clipboard):

lsof -p `pidof clipboard`
COMMAND      PID USER   FD   TYPE             DEVICE SIZE/OFF     NODE NAME
...
clipboard 265393    user  457u  unix 0x0000000066c7d124      0t0  9900913 type=STREAM

This number is going to depend on your system's file descriptor limit.

As mentioned in the comment, sometimes get_contents hangs forever instead. If you remove the return in the error branch and continue calling get_contents more errors will happen and get_contents will eventually hang.

YaLTeR commented 4 years ago

Thanks for the detailed report!

I think I need to finish updating to the newest wayland-rs before I investigate this. I should have most of the update done in the wayland-rs-master branch.

YaLTeR commented 3 years ago

I've updated wayland-rs, do you mind re-testing on latest master in case it was an old wayland-rs issue?

e00E commented 3 years ago

Still happens

YaLTeR commented 3 years ago

I investigated, looks like it's https://github.com/Smithay/wayland-rs/issues/235 again. Using the native_lib feature makes the issue go away.

YaLTeR commented 3 years ago

Upon bringing it up with the wayland-rs maintainer, looks like the issue isn't that, but runs a bit deeper: https://github.com/Smithay/wayland-rs/issues/376

YaLTeR commented 3 years ago

A possible workaround is to have a long-lived connection in wl-clipboard-rs instead of creating a new one every time, while making sure to destroy all objects when they are no longer needed.

dnut commented 2 years ago

I'm seeing the same problem in the latest version of wl-clipboard-rs. Looks like the issue is fixed in wayland-rs. Should we upgrade to wayland-client 0.29?

YaLTeR commented 2 years ago

I think it's only supposed to be fixed in wayland-client 0.30, which is the "full rework of the crate and API" version and AFAICT is not released yet.

YaLTeR commented 1 year ago

I've just finished porting to wayland-rs 0.30. Could you check if this is still an issue with the latest git commit?

dnut commented 1 year ago

I see EMFILE panics many times per day, which I believe is caused by this. I'll test this over the next few days and share my results.

YaLTeR commented 1 year ago

Hey, so how'd it go?