rust-ctor icon indicating copy to clipboard operation
rust-ctor copied to clipboard

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

Open haicoder opened this issue 1 year ago • 4 comments

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

haicoder avatar Aug 22 '24 10:08 haicoder

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

ganeshrvel avatar Sep 10 '24 16:09 ganeshrvel

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

eloff avatar Sep 18 '24 01:09 eloff

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.

mmastrac avatar Sep 18 '24 19:09 mmastrac

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

photowey avatar Oct 07 '24 16:10 photowey

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

phil-skillwon avatar Nov 07 '24 03:11 phil-skillwon

Closing in favour of a new FAQ entry -- this can be improved for sure but it's a good starting point.

https://github.com/mmastrac/rust-ctor/wiki/FAQ:-Life%E2%80%90before-and-life%E2%80%90after-main

mmastrac avatar Feb 15 '25 20:02 mmastrac