-
Notifications
You must be signed in to change notification settings - Fork 1.4k
thread.setname and context #6726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,7 +9,7 @@ pub(crate) use _thread::{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub(crate) mod _thread { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use crate::{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AsObject, Py, PyPayload, PyRef, PyResult, VirtualMachine, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| builtins::{PyDictRef, PyStr, PyTupleRef, PyType, PyTypeRef}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| builtins::{PyDictRef, PyStr, PyStrRef, PyTupleRef, PyType, PyTypeRef}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| frame::FrameRef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function::{ArgCallable, Either, FuncArgs, KwArgs, OptionalArg, PySetterValue}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| types::{Constructor, GetAttr, Representable, SetAttr}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -260,6 +260,11 @@ pub(crate) mod _thread { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[pymethod] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn locked(&self) -> bool { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.mu.is_locked() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[pymethod] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn _is_owned(&self) -> bool { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.mu.is_owned_by_current_thread() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -293,6 +298,47 @@ pub(crate) mod _thread { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
304
to
319
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential UTF-8 boundary issue when truncating thread name on Linux. The truncation at byte 15 may split a multi-byte UTF-8 character, producing an invalid UTF-8 sequence. While Consider truncating at a valid UTF-8 character boundary: Proposed fix #[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)
+ let truncated = if name.as_str().len() > 15 {
+ // Find a valid UTF-8 boundary at or before byte 15
+ let s = name.as_str();
+ let mut end = 15;
+ while end > 0 && !s.is_char_boundary(end) {
+ end -= 1;
+ }
+ CString::new(&s[..end]).unwrap_or(c_name)
} else {
c_name
};📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[cfg(not(any(target_os = "linux", target_os = "macos", windows)))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let _ = name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Get OS-level thread ID (pthread_self on Unix) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// This is important for fork compatibility - the ID must remain stable after fork | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[cfg(unix)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.