-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tracking Issue for panic_backtrace_config #93346
Comments
I think it depends on how we expose the I do worry though about how this style of API will look like in practice, I expect you'd probably have to write something like this every time you want to copy the match get_backtrace_style() {
Short => // use correct fmt flags for short
Full => // use correct fmt flags for full
// ...
} Which feels a little unfortunate. This is a strawman proposal but I would personally rather use something that can be easily expressed in one line, first idea I can come up with is this: println!("{}", backtrace.display(get_backtrace_style())); And if we were to provide an adapter API for associating a style with a backtrace when printing it then I imagine we'd want to include the style type in the backtrace module. |
For the trivial "just print it" case, I suspect a direct If you're doing something more complicated that doesn't just want to adjust the surrounding material but alter the backtrace printing directly, it may need some API design if we don't wan to stabilize some fairly low-level APIs (e.g., https://docs.rs/backtrace/latest/backtrace/fn.trace.html and similar) that may not be a good fit for std itself. I'm not sure there's a great answer here, though; I think the tradeoffs are particularly acute around this surface area and difficult to judge. |
agreed, I've felt like we should be exposing the allocation-lite backtrace printing interface we use in the panic hook for a while now.
oh interesting, I can see us going this direction, though the usage of Thinking about it though, this approach definitely reduces the value of the |
…r=yaahc Support configuring whether to capture backtraces at runtime Tracking issue: rust-lang#93346 This adds a new API to the `std::panic` module which configures whether and how the default panic hook will emit a backtrace when a panic occurs. After discussion with `@yaahc` on [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/backtrace.20lib.20vs.2E.20panic), this PR chooses to avoid adjusting or seeking to provide a similar API for the (currently unstable) std::backtrace API. It seems likely that the users of that API may wish to expose more specific settings rather than just a global one (e.g., emulating the `env_logger`, `tracing` per-module configuration) to avoid the cost of capture in hot code. The API added here could plausibly be copied and/or re-exported directly from std::backtrace relatively easily, but I don't think that's the right call as of now. ```rust mod panic { #[derive(Copy, Clone, Debug, PartialEq, Eq)] #[non_exhaustive] pub enum BacktraceStyle { Short, Full, Off, } fn set_backtrace_style(BacktraceStyle); fn get_backtrace_style() -> Option<BacktraceStyle>; } ``` Several unresolved questions: * Do we need to move to a thread-local or otherwise more customizable strategy for whether to capture backtraces? See [this comment](rust-lang#79085 (comment)) for some potential use cases for this. * Proposed answer: no, leave this for third-party hooks. * Bikeshed on naming of all the options, as usual. * Should BacktraceStyle be moved into `std::backtrace`? * It's already somewhat annoying to import and/or re-type the `std::panic::` prefix necessary to use these APIs, probably adding a second module to the mix isn't worth it. Note that PR rust-lang#79085 proposed a much simpler API, but particularly in light of the desire to fully replace setting environment variables via `env::set_var` to control the backtrace API, a more complete API seems preferable. This PR likely subsumes that one.
…yaahc Support configuring whether to capture backtraces at runtime Tracking issue: rust-lang#93346 This adds a new API to the `std::panic` module which configures whether and how the default panic hook will emit a backtrace when a panic occurs. After discussion with `@yaahc` on [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/backtrace.20lib.20vs.2E.20panic), this PR chooses to avoid adjusting or seeking to provide a similar API for the (currently unstable) std::backtrace API. It seems likely that the users of that API may wish to expose more specific settings rather than just a global one (e.g., emulating the `env_logger`, `tracing` per-module configuration) to avoid the cost of capture in hot code. The API added here could plausibly be copied and/or re-exported directly from std::backtrace relatively easily, but I don't think that's the right call as of now. ```rust mod panic { #[derive(Copy, Clone, Debug, PartialEq, Eq)] #[non_exhaustive] pub enum BacktraceStyle { Short, Full, Off, } fn set_backtrace_style(BacktraceStyle); fn get_backtrace_style() -> Option<BacktraceStyle>; } ``` Several unresolved questions: * Do we need to move to a thread-local or otherwise more customizable strategy for whether to capture backtraces? See [this comment](rust-lang#79085 (comment)) for some potential use cases for this. * Proposed answer: no, leave this for third-party hooks. * Bikeshed on naming of all the options, as usual. * Should BacktraceStyle be moved into `std::backtrace`? * It's already somewhat annoying to import and/or re-type the `std::panic::` prefix necessary to use these APIs, probably adding a second module to the mix isn't worth it. Note that PR rust-lang#79085 proposed a much simpler API, but particularly in light of the desire to fully replace setting environment variables via `env::set_var` to control the backtrace API, a more complete API seems preferable. This PR likely subsumes that one.
@rust-lang/libs-api I'd like to propose to stabilize these APIs as-is. The API matches what we have done with environment variables for a long time, just a bit more type-safe. There's more that can be done here (see the posts by @yaahc above), but I don't think any of this should be a blocker -- given in particular that this feature is on the critical path for reducing the use of |
|
Oh, interesting. On the one hand it seems strange if a Maybe the enum and methods should be moved to |
Sorry for interfering, though I'd suggest previous |
Usually, we omit |
We are getting closer to set_var being unsafe. It would be good to have this stable soon so that people can migrate away from a soon-to-be-unsafe API to something safe.
|
What does t-libs-api think is the better choice here? I don't have a strong opinion, but this API should ideally land in time for the edition.
Cc @rust-lang/libs-api |
We discussed this in the libs-api meeting today. The main point of contention is that we have 2 separate points where we configure the "backtrace mode":
Our conclusion is that we should provide APIs to individually control both of these options: // std::panic
fn set_panic_backtrace_style(style: BacktraceStyle);
fn panic_backtrace_style() -> BacktraceStyle;
// std::backtrace
fn set_backtrace_style(style: BacktraceStyle);
fn backtrace_style() -> BacktraceStyle; Some notable differences between this API and the existing one:
|
@Amanieu thanks! Will these use the same |
My understanding is that the consideration of making this possible in a thread-local manner has been pushed to third party libraries - meaning there is confidence that the standard library solution would leave sufficient hooks for third party libraries to do this efficiently - is that correct? Is that also how this would be handled in an async context? A good usecase is |
Feature gate:
#![feature(panic_backtrace_config)]
This is a tracking issue for configuring the capture and display of backtraces in the default panic hook, as well as exposing that configuration to third-party libraries for usage.
Public API
This API is intended to form part of the strategy for addressing the unsoundness of the
std::env::set_var
API on some platforms (mostly non-Windows). See #92431 (comment) for a summary.Steps / History
Unresolved Questions
std::backtrace
?std::panic::
prefix necessary to use these APIs, probably adding a second module to the mix isn't worth it.get_backtrace_style
be exposed?Option<BacktraceStyle>
return type looks a little weird, and may not mean the intuitive thing --None
representsUnsupported
, not "not set" or "don't print backtraces", as might be initially assumed.std::panic::report_backtrace(&mut dyn Write)
instead which internally knows how to format things.The text was updated successfully, but these errors were encountered: