From e35d0affe796e629845779b8225a87617cc9dc53 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 9 May 2026 23:29:55 +0200 Subject: [PATCH] kernel_copy tests: properly join background threads --- .../std/src/sys/io/kernel_copy/linux/tests.rs | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/library/std/src/sys/io/kernel_copy/linux/tests.rs b/library/std/src/sys/io/kernel_copy/linux/tests.rs index 9b2d9cbef990..ba0828039ddd 100644 --- a/library/std/src/sys/io/kernel_copy/linux/tests.rs +++ b/library/std/src/sys/io/kernel_copy/linux/tests.rs @@ -166,10 +166,14 @@ fn bench_file_to_socket_copy(b: &mut test::Bencher) { let mut sink = crate::net::TcpStream::connect(sink_drainer.local_addr().unwrap()).unwrap(); let mut sink_drainer = sink_drainer.accept().unwrap().0; - crate::thread::spawn(move || { + let t = crate::thread::spawn(move || { let mut sink_buf = vec![0u8; 1024 * 1024]; loop { - sink_drainer.read(&mut sink_buf[..]).unwrap(); + let n = sink_drainer.read(&mut sink_buf[..]).unwrap(); + if n == 0 { + // EOF + break; + } } }); @@ -178,6 +182,9 @@ fn bench_file_to_socket_copy(b: &mut test::Bencher) { src.seek(SeekFrom::Start(0)).unwrap(); assert_eq!(BYTES as u64, io::copy(&mut src, &mut sink).unwrap()); }); + + drop(sink); // close our end, so that the thread goes down + t.join().unwrap(); } #[bench] @@ -196,10 +203,14 @@ fn bench_file_to_uds_copy(b: &mut test::Bencher) { let (mut sink, mut sink_drainer) = crate::os::unix::net::UnixStream::pair().unwrap(); - crate::thread::spawn(move || { + let t = crate::thread::spawn(move || { let mut sink_buf = vec![0u8; 1024 * 1024]; loop { - sink_drainer.read(&mut sink_buf[..]).unwrap(); + let n = sink_drainer.read(&mut sink_buf[..]).unwrap(); + if n == 0 { + // EOF + break; + } } }); @@ -208,6 +219,9 @@ fn bench_file_to_uds_copy(b: &mut test::Bencher) { src.seek(SeekFrom::Start(0)).unwrap(); assert_eq!(BYTES as u64, io::copy(&mut src, &mut sink).unwrap()); }); + + drop(sink); // close our end, so that the thread goes down + t.join().unwrap(); } #[cfg(any(target_os = "linux", target_os = "android"))]