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

Close file descriptors of pipes after use #2591

Merged
merged 3 commits into from
Aug 24, 2021
Merged

Conversation

blyxxyz
Copy link
Contributor

@blyxxyz blyxxyz commented Aug 23, 2021

nix::unistd::pipe returns RawFds which have to be closed at some point, otherwise they are leaked. Wrapping them in Files does the trick.

The leaked file descriptors can be observed by running uucat /dev/null /dev/null /dev/null - in one terminal (the - ensures it doesn't exit immediately) and then running lsof -p $(pidof uucat) in another.

It's possible to implement this without the unsafe keyword by writing a wrapper that calls nix::unistd::close when it's dropped, but that function is technically also unsafe and probably will be marked as such in the future, so it's better to be upfront. See nix-rust/nix#1421.

@miDeb
Copy link
Contributor

miDeb commented Aug 23, 2021

Thanks, this looks good! Could you add a test? I fixed a similar bug some time ago (#2330), and the test I added looked like this:

#[cfg(target_os = "linux")]
#[test]
fn test_closes_file_descriptors() {
    new_ucmd!()
        .arg("-r")
        .arg("--reflink=auto")
        .arg("dir_with_10_files/")
        .arg("dir_with_10_files_new/")
        .with_limit(Resource::NOFILE, 9, 9)
        .succeeds();
}

The important part is .with_limit(Resource::NOFILE, 9, 9), which limits the utility to max. 9 file descriptors. The test then tries to do something with a directory of 10 files, which can only succeed if file descriptors are closed regularly.

Would it be possible to add a similar test for the bugs you fixed? It's not extremely important, but it's nice to have regression tests, especially for subtle bugs like this.

@blyxxyz
Copy link
Contributor Author

blyxxyz commented Aug 23, 2021

Good idea, I hadn't thought of resource limits.

Both cat and wc were actually set up to fall back if pipe() fails, which makes this hard to test. I think it's ok to change that, pipe() only fails in exceptional circumstances. But in the case of wc it'd require overhauling the control flow, and I don't know if that's worth it.

This is the test I'd write otherwise, for posterity:

#[test]
#[cfg(target_os = "linux")]
fn test_closes_file_descriptors() {
    new_ucmd!()
        .args(&[
            "-c",
            "/dev/stdin",
            "/dev/stdin",
            "/dev/stdin",
            "/dev/stdin",
            "/dev/stdin",
        ])
        .with_limit(Resource::NOFILE, 9, 9)
        .succeeds();
}

@miDeb miDeb merged commit 516c531 into uutils:master Aug 24, 2021
@miDeb
Copy link
Contributor

miDeb commented Aug 24, 2021

Thanks!

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