Repository: RustPython/RustPython
File: crates/vm/src/stdlib/thread.rs (Rust)

Code before:
        fn __exit__(&self, _args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
            self.release(vm)
        }
    }

    impl Representable for RLock {
        #[inline]
        fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
            repr_lock_impl!(zelf)
        }
    }

    /// Get thread identity - uses pthread_self() on Unix for fork compatibility
    #[pyfunction]
    pub fn get_ident() -> u64 {
        current_thread_id()
    }

    /// Set the name of the current thread
    #[pyfunction]
    fn set_name(name: PyStrRef) {
        #[cfg(target_os = "linux")]
        {
            use std::ffi::CString;
            if let Ok(c_name) = CString::new(name.as_str()) {
                // pthread_setname_np on Linux has a 16-byte limit including null terminator
                let truncated = if c_name.as_bytes().len() > 15 {
                    CString::new(&c_name.as_bytes()[..15]).unwrap_or(c_name)
                } else {
                    c_name
                };
                unsafe {
                    libc::pthread_setname_np(libc::pthread_self(), truncated.as_ptr());
                }
            }
        }
        #[cfg(target_os = "macos")]
        {
            use std::ffi::CString;
            if let Ok(c_name) = CString::new(name.as_str()) {
                unsafe {
                    libc::pthread_setname_np(c_name.as_ptr());
                }
            }
        }
        #[cfg(windows)]
        {
            // Windows doesn't have a simple pthread_setname_np equivalent
            // SetThreadDescription requires Windows 10+
            let _ = name;
        }

Review comment (suggestion):
```suggestion
                // pthread_setname_np on Linux has a 16-byte limit including null terminator
                // TODO: Potential UTF-8 boundary issue when truncating thread name on Linux.
                // https://github.com/RustPython/RustPython/pull/6726/changes#r2689379171
```

Code after:
        fn __exit__(&self, _args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
            self.release(vm)
        }
    }

    impl Representable for RLock {
        #[inline]
        fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
            repr_lock_impl!(zelf)
        }
    }

    /// Get thread identity - uses pthread_self() on Unix for fork compatibility
    #[pyfunction]
    pub fn get_ident() -> u64 {
        current_thread_id()
    }

    /// Set the name of the current thread
    #[pyfunction]
    fn set_name(name: PyStrRef) {
        #[cfg(target_os = "linux")]
        {
            use std::ffi::CString;
            if let Ok(c_name) = CString::new(name.as_str()) {
                // pthread_setname_np on Linux has a 16-byte limit including null terminator
                // TODO: Potential UTF-8 boundary issue when truncating thread name on Linux.
                // https://github.com/RustPython/RustPython/pull/6726/changes#r2689379171
                let truncated = if c_name.as_bytes().len() > 15 {
                    CString::new(&c_name.as_bytes()[..15]).unwrap_or(c_name)
                } else {
                    c_name
                };
                unsafe {
                    libc::pthread_setname_np(libc::pthread_self(), truncated.as_ptr());
                }
            }
        }
        #[cfg(target_os = "macos")]
        {
            use std::ffi::CString;
            if let Ok(c_name) = CString::new(name.as_str()) {
                unsafe {
                    libc::pthread_setname_np(c_name.as_ptr());
                }
            }
        }
        #[cfg(windows)]
        {
            // Windows doesn't have a simple pthread_setname_np equivalent
            // SetThreadDescription requires Windows 10+