rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.72k stars 12.64k forks source link

Using thread::sleep inside panic hook aborts panic hook #124965

Open gucki opened 5 months ago

gucki commented 5 months ago

I tried this code:

    panic::set_hook(Box::new(|panic_info| {
      log::debug!("step 1");
      thread::sleep(time::Duration::from_secs(3));
      log::debug!("step 2");
      if let Err(e) = Command::new("...").spawn() {
        log::error!(e);
      }
      log::debug!("step 3");
      process::exit(0);
    }));

I expected to see this happen: all the code in the panic handler is executed.

Instead, this happened: with the thread::sleep, the execution of the panic handler is silently stopped at this step (the following code is simply ignored!). When removing the thread::sleep, the panic handler gets executed as expected.

Meta

cargo 1.77.2 (e52e36006 2024-03-26) release: 1.77.2 commit-hash: e52e360061cacbbeac79f7f1215a7a90b6f08442 commit-date: 2024-03-26 host: x86_64-unknown-linux-gnu libgit2: 1.7.2 (sys:0.18.2 vendored) libcurl: 8.5.0-DEV (sys:0.4.70+curl-8.5.0 vendored ssl:OpenSSL/1.1.1w) ssl: OpenSSL 1.1.1w 11 Sep 2023 os: Debian 12.0.0 [64-bit]

cargo build --target x86_64-pc-windows-gnu

correabuscar commented 5 months ago

Seems to work, am I missing something? ah, windows target... It works in wine64 on nixos wine64 target/x86_64-pc-windows-gnu/debug/pthread_atfork_try2.exe

DEBUG - step 1
DEBUG - step 2
ERROR - Invalid handle. (os error 6)
DEBUG - step 3
exit code: 0

It works on Windows 11 23H2 too(I even went back and added log::logger):

c:\1>pthread_atfork_try2.exe
DEBUG - step 1
DEBUG - step 2
ERROR - Access is denied. (os error 5)
DEBUG - step 3

c:\1>ver

Microsoft Windows [Version 10.0.22631.3447]
double checked rust version in use is(before compiling): ``` info: component 'rust-std' for target 'x86_64-pc-windows-gnu' is up to date cargo 1.77.2 (e52e36006 2024-03-26) release: 1.77.2 commit-hash: e52e360061cacbbeac79f7f1215a7a90b6f08442 commit-date: 2024-03-26 host: x86_64-unknown-linux-gnu libgit2: 1.7.2 (sys:0.18.2 vendored) libcurl: 8.5.0-DEV (sys:0.4.70+curl-8.5.0 vendored ssl:OpenSSL/1.1.1w) ssl: OpenSSL 1.1.1w 11 Sep 2023 os: NixOS 24.5.0 [64-bit] ```
workingjubilee commented 5 months ago

Wine is not an emulator.