rust-lang / rust-lang/rust

catch_unwind docs are unclear about panic hook

Open
#122,464 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-docs T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Location

std::panic::catch_unwind

Summary

The docs for std::panic::catch_unwind notes section:

Notes

Note that this function might not catch all panics in Rust. A panic in Rust is not always implemented via unwinding, but can be implemented by aborting the process as well. This function only catches unwinding panics, not those that abort the process.

Note that if a custom panic hook has been set, it will be invoked before the panic is caught, before unwinding.

Also note that unwinding into Rust code with a foreign exception (e.g. an exception thrown from C++ code) is undefined behavior.

The second paragraph says "if a custom panic hook has been set". This implies a simple program like the following will not print a panic message, but it does:

fn main() {
    let _ = std::panic::catch_unwind(|| panic!());
}
// output
thread 'main' panicked at src/main.rs:2:41:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

This is the default hook, there is no need to set a custom one.

The text could be changed to something like "if a panic hook is set", as well as further discussion of the panic hook, which is covered in the panic! docs, but none of the docs for panic!, set_hook, or take_hook mention catch_unwind. There's no discussion in the parent panic module.

In typical usage catch_unwind will involve set_hook and take_hook, so the docs should cover this.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the std::panic::catch_unwind notes section, then compare the panic!, set_hook, take_hook, and parent panic documentation mentioned in the issue. Clarify that the panic hook runs before unwinding even when the default hook is used, and add the relevant cross-references or discussion so typical catch_unwind usage is accurately documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.