From fa6c636099fa73442b8c9d9a2ba83cb2968fe5d0 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:00:18 +0530 Subject: [PATCH 1/2] pgrep: retry flaky test_thread_ids The test took a single /proc//task snapshot from a newly spawned thread. That directory can lag, so the new tid was sometimes missing and the test failed at random in PR CI. Poll until both the main and new thread ids are visible, or fail with the snapshot after two seconds. Fixes #471 Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> --- src/uu/pgrep/src/process.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/uu/pgrep/src/process.rs b/src/uu/pgrep/src/process.rs index 211f8a51..d42cc6b7 100644 --- a/src/uu/pgrep/src/process.rs +++ b/src/uu/pgrep/src/process.rs @@ -902,15 +902,25 @@ unknown /dev/tty 4 1-63 console"#; #[test] #[cfg(target_os = "linux")] fn test_thread_ids() { - let main_tid = rustix::thread::gettid().as_raw_nonzero().get() as u64; + let main_tid = rustix::thread::gettid().as_raw_nonzero().get() as usize; std::thread::spawn(move || { - let mut pid_entry = ProcessInformation::current_process_info().unwrap(); - let thread_ids = pid_entry.thread_ids(); - - assert!(thread_ids.contains(&(main_tid as usize))); - - let new_thread_tid = rustix::thread::gettid().as_raw_nonzero().get() as u64; - assert!(thread_ids.contains(&(new_thread_tid as usize))); + let new_thread_tid = rustix::thread::gettid().as_raw_nonzero().get() as usize; + // /proc//task can lag a newly started thread, and a single + // WalkDir snapshot may miss it (https://github.com/uutils/procps/issues/471). + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2); + loop { + let mut pid_entry = ProcessInformation::current_process_info().unwrap(); + let thread_ids = pid_entry.thread_ids(); + if thread_ids.contains(&main_tid) && thread_ids.contains(&new_thread_tid) { + return; + } + if std::time::Instant::now() >= deadline { + panic!( + "thread_ids missing main {main_tid} or new {new_thread_tid}: {thread_ids:?}" + ); + } + std::thread::sleep(std::time::Duration::from_millis(5)); + } }) .join() .unwrap(); From 080b5c119c5cae6d51ee7230e1b6e99b28909e0f Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Wed, 11 Nov 2026 14:20:59 +0530 Subject: [PATCH 2/2] pgrep: explain why test_thread_ids retries The new thread is already running when it reads its own task directory. The miss comes from readdir on /proc//task skipping entries while other test threads exit concurrently, not from the directory lagging. Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> --- src/uu/pgrep/src/process.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/uu/pgrep/src/process.rs b/src/uu/pgrep/src/process.rs index d42cc6b7..8cc62110 100644 --- a/src/uu/pgrep/src/process.rs +++ b/src/uu/pgrep/src/process.rs @@ -905,8 +905,9 @@ unknown /dev/tty 4 1-63 console"#; let main_tid = rustix::thread::gettid().as_raw_nonzero().get() as usize; std::thread::spawn(move || { let new_thread_tid = rustix::thread::gettid().as_raw_nonzero().get() as usize; - // /proc//task can lag a newly started thread, and a single - // WalkDir snapshot may miss it (https://github.com/uutils/procps/issues/471). + // This thread is already running, but readdir on /proc//task + // can skip entries while other test threads exit concurrently, so a + // single snapshot may miss a tid (https://github.com/uutils/procps/issues/471). let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2); loop { let mut pid_entry = ProcessInformation::current_process_info().unwrap();