Skip to content

Thread panic message to stderr kills windowed mode Windows binaries #25643

Closed
@rsaarelm

Description

@rsaarelm

Windows binaries built with the -mwindows linker flag can't have any output to stdout or stderr in them or Windows will instantly terminate the program. I want to have windowed mode binaries for GUI applications, since without the windowed mode flag the binary will spawn a visible console window in addition to the GUI window every time it's run.

Panicing threads seem to cause an unconditional print to stderr. I'm trying to write a panic handler that will pop up a Windows message dialog instead of printing to stdout when the program terminates with a panic, but currently can't do that because windowed mode programs die at the point of the panic and the handler is never reached.

To test this, write panic.rs:

fn f() { panic!("This kills the Windows"); }

fn main() {
    match std::thread::Builder::new().spawn(f).unwrap().join() {
        Err(_) => { std::fs::File::create("panic_log.txt"); }
        _ => {}
    }
}

Build it with rustc -C link-args=-mwindows panic.rs. Then run panic.exe from the Windows explorer. The program terminates and the panic_log.txt file never shows up. (It does seem to work if you run it from the Command Prompt, but end users will be clicking an icon.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    O-windowsOperating system: Windows

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions