重命名 Rust 中的 ‘main’ 线程

huangapple go评论68阅读模式
英文:

Rename 'main' thread in Rust

问题

I am working on an application that executes many processes, which at the same time use many threads. When something fails and the thread panics, rust outputs: thread 'main' panicked at 'explicit panic'. I would like to use this messages for debugging purposes, being able to identify the panicking process by its name, but in this case all processes have the 'main' thread name.

How can I change the main thread name?

I know I can change the name of a child thread using the std::thread::Builder, but this does not work for the current thread.

I have also tried using pthread_setname_np call (I am using a Linux system):

use libc::{pthread_self, pthread_setname_np};
use std::{ffi::CString, thread};

fn main() {

    // Set the name of the main thread
    let name = CString::new("new_name").unwrap();
    unsafe {
        pthread_setname_np(pthread_self(), name.as_ptr());
    }

    panic!();
}

Executing this with cargo run still panics with the main name.

英文:

I am working on an application that executes many processes, which at the same time use many threads. When something fails and the thread panics, rust outputs: thread 'main' panicked at 'explicit panic'. I would like to use this messages for debugging purposes, being able to identify the panicking process by its name, but in this case all processes have the 'main' thread name.

How can I change the main thread name?

I know I can change the name of a child thread using the std::thread::Builder, but this does not work for the current thread.

I have also tried using pthread_setname_np call (I am using a Linux system):

use libc::{pthread_self, pthread_setname_np};
use std::{ffi::CString, thread};

fn main() {

    // Set the name of the main thread
    let name = CString::new("new_name").unwrap();
    unsafe {
        pthread_setname_np(pthread_self(), name.as_ptr());
    }

    panic!();
}

Executing this with cargo run still panics with the main name.

答案1

得分: 3

I'm afraid this is impossible: the thread name is managed independently from the OS, and it is set only in two places in the standard library: when setting up main(), and when spawning a thread. There is no public API that changes it for the current thread (you can ask for one if you believe this is important).

You can spawn a new thread and immediately join() it.

英文:

I'm afraid this is impossible: the thread name is managed independently from the OS, and it is set only in two places in the standard library: when setting up main(), and when spawning a thread. There is no public API that changes it for the current thread (you can ask for one, if you believe this is important).

You can spawn a new thread and immediately join() it.

答案2

得分: 2

以下是您要翻译的内容:

一种在确定哪个进程崩溃的方法是设置自定义的 panic 处理程序。这在 Deno 运行时中有一个很好的示例:

https://github.com/denoland/deno/pull/13145/files

以下是如何执行的简短示例:

fn main() {
    set_panic_hook(format!("程序 {}", 1));
    panic!("发生了一些糟糕的事情");
}

fn set_panic_hook(process_name: String) {
    let orig_hook = std::panic::take_hook();
    std::panic::set_hook(Box::new(move |panic_info| {
        eprintln!("\nPanic in process: {}", process_name);
        orig_hook(panic_info);
        std::process::exit(1);
    }));
}

Rust Playground 示例

显示以下输出:

Panic in process: 程序 1
thread 'main' panicked at '发生了一些糟糕的事情', src/main.rs:3:5
英文:

One way to put detail on which process crashed is to set up a custom panic handler. A good example of this is in the Deno runtime:

https://github.com/denoland/deno/pull/13145/files

Here's a short sample of how to do it:

fn main() {
    set_panic_hook(format!("Program {}", 1));
    panic!("something bad happened");
}

fn set_panic_hook(process_name: String) {
    let orig_hook = std::panic::take_hook();
    std::panic::set_hook(Box::new(move |panic_info| {
        eprintln!("\nPanic in process: {process_name}");
        orig_hook(panic_info);
        std::process::exit(1);
    }));
}

Rust Playground Example

Displays this output:

Panic in process: Program 1
thread 'main' panicked at 'something bad happened', src/main.rs:3:5

huangapple
  • 本文由 发表于 2023年4月17日 01:28:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定