mmastrac / rust-ctor

Module initialization/global constructor functions for Rust
Apache License 2.0
742 stars 49 forks source link

ctor cause `fatal runtime error: thread::set_current should only be called once per thread` #307

Open haicoder opened 2 months ago

haicoder commented 2 months ago

rust version

rustc 1.82.0-nightly (5aea14073 2024-08-20)

system

Debian GNU/Linux 10 (buster)

ctor version

0.2.8

Code

// src/bin/test.rs
fn main() {
}

#[ctor::ctor]
fn setup() {
    println!("enter")
}

Output

enter
fatal runtime error: thread::set_current should only be called once per thread
ganeshrvel commented 1 month ago

Same issue occurred in my project after updating to Rust 1.81.0. I resolved it using the libc-print crate.

eloff commented 1 month ago

I'm seeing the same thing after updating to Rust 1.81.0

mmastrac commented 1 month ago

Unfortunately the builtin println is not safe to use in ctor or dtor -- libc_print is specifically designed for this: https://crates.io/crates/libc-print.

photowey commented 1 month ago

I'm seeing the same thing after updating to Rust 1.81.0.

phil-skillwon commented 1 day ago

I'm seeing the same thing after updating to Rust 1.81.0