Why does an atexit handler panic when it accesses stdout?

Viewed 1124

The Rust program below panics when it accesses stdout in the atexit handler.

extern crate libc;

extern "C" fn bye() {
    println!("bye");
}

fn main() {
    println!("hello");
    unsafe { libc::atexit(bye) };
}

Output:

hello
thread '<main>' panicked at 'cannot access stdout during shutdown', ../src/libcore/option.rs:298
fatal runtime error: Could not unwind stack, error = 5
An unknown error occurred

It seems to me that this registration should run before our atexit registration, so this line in the handler should run only after our custom handler. Thus it should not panic.

1 Answers
Related