diff --git a/src/uu/pgrep/src/process.rs b/src/uu/pgrep/src/process.rs index 211f8a51..8cc62110 100644 --- a/src/uu/pgrep/src/process.rs +++ b/src/uu/pgrep/src/process.rs @@ -902,15 +902,26 @@ 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; + // 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(); + 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();