From 42a708083a59ece9599723f69536ddb6e987e6e4 Mon Sep 17 00:00:00 2001 From: Georgii Rylov Date: Thu, 10 Aug 2023 14:06:41 +0100 Subject: [PATCH 1/2] Fix a pthread_t handle leak #114610 --- library/std/src/sys/wasi/thread.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/library/std/src/sys/wasi/thread.rs b/library/std/src/sys/wasi/thread.rs index dbad425976a3..99717613f57c 100644 --- a/library/std/src/sys/wasi/thread.rs +++ b/library/std/src/sys/wasi/thread.rs @@ -47,6 +47,7 @@ cfg_if::cfg_if! { stack_size: libc::size_t, ) -> ffi::c_int; pub fn pthread_attr_destroy(attr: *mut pthread_attr_t) -> ffi::c_int; + pub fn pthread_detach(thread: pthread_t) -> ffi::c_int; } } @@ -178,6 +179,17 @@ impl Thread { } } +cfg_if::cfg_if! { + if #[cfg(target_feature = "atomics")] { + impl Drop for Thread { + fn drop(&mut self) { + let ret = unsafe { libc::pthread_detach(self.id) }; + debug_assert_eq!(ret, 0); + } + } + } +} + pub fn available_parallelism() -> io::Result { unsupported() } From 9b00e5f06f51703c5294af602959d3f92c095aa7 Mon Sep 17 00:00:00 2001 From: Georgii Rylov Date: Wed, 16 Aug 2023 11:19:22 +0100 Subject: [PATCH 2/2] address comments --- library/std/src/sys/wasi/thread.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/library/std/src/sys/wasi/thread.rs b/library/std/src/sys/wasi/thread.rs index 99717613f57c..a0eefa8811a3 100644 --- a/library/std/src/sys/wasi/thread.rs +++ b/library/std/src/sys/wasi/thread.rs @@ -54,6 +54,13 @@ cfg_if::cfg_if! { pub struct Thread { id: libc::pthread_t, } + + impl Drop for Thread { + fn drop(&mut self) { + let ret = unsafe { libc::pthread_detach(self.id) }; + debug_assert_eq!(ret, 0); + } + } } else { pub struct Thread(!); } @@ -179,17 +186,6 @@ impl Thread { } } -cfg_if::cfg_if! { - if #[cfg(target_feature = "atomics")] { - impl Drop for Thread { - fn drop(&mut self) { - let ret = unsafe { libc::pthread_detach(self.id) }; - debug_assert_eq!(ret, 0); - } - } - } -} - pub fn available_parallelism() -> io::Result { unsupported() }