From b8f906f52c0eb1ade509b46fe65373d68b351c63 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Thu, 19 Nov 2026 09:34:51 +0100 Subject: [PATCH] tests: count descriptors in a process of their own host::tests::a_large_directory_costs_no_descriptors lists /proc/self/fd 1502 times while the other unit tests of the fat crate run in other threads of the same process. procfs takes a reference on every file it lists and on every file whose link it reads. When another test closes its image in that moment, the scanner puts the last reference and the BSD lock is only dropped when the scanner returns to user space. The other test reopens the image right away and fails with EBUSY. Move the test into a test binary of its own, so no other test runs in the process that lists /proc/self/fd. walk_tree() is private to the fat crate, so the test now copies the directory into a volume with copy_tree_with_progress() and counts in the progress callback. That callback runs while the walk holds its pins and the file it copies, so the limit goes up from four descriptors to five. Signed-off-by: Christian Brauner (Amutable) --- src/fat/host.rs | 45 -------------------------- tests/descriptors.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 45 deletions(-) create mode 100644 tests/descriptors.rs diff --git a/src/fat/host.rs b/src/fat/host.rs index 9fdd7d2..267734a 100644 --- a/src/fat/host.rs +++ b/src/fat/host.rs @@ -833,51 +833,6 @@ mod tests { assert!(from_start.read(&mut [0u8; 4]).is_err()); } - /// The descriptors of this process that refer to something below - /// `root`. Other tests run in other threads of this process with - /// descriptors of their own, so only ours are counted. - fn descriptors_below(root: &Path) -> usize { - fs::read_dir("/proc/self/fd") - .unwrap() - .filter(|e| match fs::read_link(e.as_ref().unwrap().path()) { - Ok(target) => target.starts_with(root), - Err(_) => false, - }) - .count() - } - - /// A directory of 1500 entries, more than the usual soft limit of 1024 - /// descriptors: the walk pins one entry at a time and holds one - /// directory per level, so it never needs more than a handful. - #[test] - fn a_large_directory_costs_no_descriptors() { - let tmp = TempDir::new("walk-large"); - let root = tmp.path(); - fs::create_dir(root.join("big")).unwrap(); - for i in 0..1500 { - fs::write(root.join(format!("big/f{:04}", i)), b"x").unwrap(); - } - let fd = open_dir(root).unwrap(); - let mut seen = 0; - let mut most = 0; - walk_tree(fd.as_fd(), "/", false, &mut |_, entry| { - if entry.is_file() { - let mut text = String::new(); - entry.open_file().unwrap().read_to_string(&mut text).unwrap(); - assert_eq!(text, "x"); - } - seen += 1; - most = most.max(descriptors_below(root)); - Ok(true) - }) - .unwrap(); - assert_eq!(seen, 1501); - // The test's own root descriptor, the walk's dup of it, the pinned - // "big" and the pinned entry being visited. - assert!(most <= 4, "{} descriptors open below the root", most); - assert_eq!(descriptors_below(root), 1); - } - /// An entry removed after the directory was listed but before the walk /// reached it is left out; the walk goes on with the next one. #[test] diff --git a/tests/descriptors.rs b/tests/descriptors.rs new file mode 100644 index 0000000..f40553a --- /dev/null +++ b/tests/descriptors.rs @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +//! How many descriptors copying a tree holds open, counted in +//! /proc/self/fd. +//! +//! This test is the only one in its binary, so it has a process of its +//! own. Listing /proc/self/fd and reading its links takes a reference on +//! every open file of the process for a moment. When another thread closes +//! its last descriptor of a file in that moment, the file is only released +//! once the listing thread has dropped its reference and returned to user +//! space, and so is the BSD lock that file holds. A test in another thread +//! that opens the same image again right away then finds it locked +//! (EBUSY). Keep other tests out of this file. + +mod common; + +use std::fs; +use std::os::fd::AsFd; +use std::path::Path; + +use common::Temp; +use fat::device::{Access, Device}; +use fat::format::{self, FormatOptions}; +use fat::host; +use fat::populate::CopyOptions; +use fat::volume::Volume; + +/// The number of files in the directory the test copies, more than the +/// usual soft limit of 1024 descriptors. +const FILES: u64 = 1500; + +/// The size of the image the tree is copied to. +const IMAGE_SIZE: u64 = 64 << 20; + +/// The descriptors of this process that refer to something below `root`. +fn descriptors_below(root: &Path) -> usize { + fs::read_dir("/proc/self/fd") + .unwrap() + .filter(|e| match fs::read_link(e.as_ref().unwrap().path()) { + Ok(target) => target.starts_with(root), + Err(_) => false, + }) + .count() +} + +/// The walk pins one entry at a time and holds one directory per level, +/// so copying a large directory never needs more than a handful of +/// descriptors, and none is left open afterwards. +#[test] +fn a_large_directory_costs_no_descriptors() { + let tree = Temp::dir(); + let root = tree.path(); + fs::create_dir(root.join("big")).unwrap(); + for i in 0..FILES { + fs::write(root.join(format!("big/f{:04}", i)), b"x").unwrap(); + } + let image = Temp::image(IMAGE_SIZE); + format::format( + &Device::open(image.path(), Access::ReadWrite).unwrap(), + &FormatOptions::default(), + ) + .unwrap(); + let mut v = Volume::open(Device::open(image.path(), Access::ReadWrite).unwrap()).unwrap(); + let fd = host::open_dir(root).unwrap(); + let mut most = 0; + let report = v + .copy_tree_with_progress(fd.as_fd(), "/", &CopyOptions::default(), &mut |_| { + most = most.max(descriptors_below(root)); + }) + .unwrap(); + assert_eq!((report.directories, report.files, report.bytes), (1, FILES, FILES)); + // The test's own root descriptor, the walk's dup of it, the pinned + // "big", the pinned entry being copied and the file opened from it. + assert!(most <= 5, "{} descriptors open below the root", most); + assert_eq!(descriptors_below(root), 1); +}