containerd / ttrpc-rust

Rust implementation of ttrpc (GRPC for low-memory environments)
Apache License 2.0
195 stars 45 forks source link

Fix bug: Async client may not close #236

Open jokemanfire opened 1 month ago

jokemanfire commented 1 month ago

when just new a Client,the fd will increase. I also think the lifetime in async client is too long. also in sync #225 .

I obtained the following test code for this phenomenon:

fn get_describe_numbers(pid: &str) -> i64 {
    let proc_dir = format!("/proc/{}/fd", pid);
    let mut cnt = 0;
    match read_dir(PathBuf::from(proc_dir)) {
        Ok(entries) => {
            for entry in entries {
                match entry {
                    Ok(e) => {
                        cnt += 1;
                    }
                    Err(e) => {
                        eprintln!("Error reading directory entry: {}", e);
                    }
                }
            }
        }
        Err(e) => {
            eprintln!("Error reading directory: {}", e);
        }
    }
    cnt
}

#[cfg(unix)]
#[tokio::main(flavor = "current_thread")]
async fn main() {
    let pid = process::id().to_string();
    let pre_fds = get_describe_numbers(pid.as_str());
    for i in 0..10 {
        let now_fds = get_describe_numbers(pid.as_str());
        if now_fds != pre_fds {
            println!("fd is not release pre_fd {pre_fds:?}  now_fd {now_fds:?}");
            // pre_fds = now_fds
        }
        println!(
            "-------------now fd number is {now_fds:?}---------------------------------------"
        );
        let c = Client::connect(utils::SOCK_ADDR).unwrap();
    }
    let now_fds = get_describe_numbers(pid.as_str());
    if now_fds != pre_fds {
        println!("fd is not release pre_fd {pre_fds:?}  now_fd {now_fds:?}");
        // pre_fds = now_fds
    }

    println!("over");
}
jokemanfire commented 3 days ago

let conn = Connection::new(stream, delegate);

I think connection is client's member, the lifetime of connection should not above the client struct, if client is drop as well as conn should exit. If conn is lock may cause thread leak.

wllenyj commented 2 days ago

I am not sure, is not the fd closed by the Connection. If true, there is now a risk of a double close. If any fd is opened between two closes, it will be closed silently.

jokemanfire commented 2 days ago

I am not sure, is not the fd closed by the Connection. If true, there is now a risk of a double close. If any fd is opened between two closes, it will be closed silently.

I check the connection , but there's no fd release, please check it again if i missed.