Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

## [0.24.17] - 2025-11-02

### Fixed
- Preserve captured backtraces when wrapping `AppError` instances through
`ResultExt::context` by sharing the snapshot instead of attempting to clone
`std::backtrace::Backtrace`.

## [0.24.16] - 2025-11-01

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[package]
name = "masterror"
version = "0.24.16"
version = "0.24.17"
rust-version = "1.90"
edition = "2024"
license = "MIT OR Apache-2.0"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ The build script keeps the full feature snippet below in sync with

~~~toml
[dependencies]
masterror = { version = "0.24.16", default-features = false }
masterror = { version = "0.24.17", default-features = false }
# or with features:
# masterror = { version = "0.24.16", features = [
# masterror = { version = "0.24.17", features = [
# "std", "axum", "actix", "openapi",
# "serde_json", "tracing", "metrics", "backtrace",
# "sqlx", "sqlx-migrate", "reqwest", "redis",
Expand Down
41 changes: 34 additions & 7 deletions src/app_error/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ pub struct ErrorInner {
pub details: Option<String>,
pub source: Option<Arc<dyn CoreError + Send + Sync + 'static>>,
#[cfg(feature = "backtrace")]
pub backtrace: Option<Backtrace>,
pub backtrace: Option<Arc<Backtrace>>,
#[cfg(feature = "backtrace")]
pub captured_backtrace: OnceLock<Option<Backtrace>>,
pub captured_backtrace: OnceLock<Option<Arc<Backtrace>>>,
telemetry_dirty: AtomicBool,
#[cfg(feature = "tracing")]
tracing_dirty: AtomicBool
Expand All @@ -110,9 +110,9 @@ const BACKTRACE_STATE_DISABLED: u8 = 2;
static BACKTRACE_STATE: AtomicU8 = AtomicU8::new(BACKTRACE_STATE_UNSET);

#[cfg(feature = "backtrace")]
fn capture_backtrace_snapshot() -> Option<Backtrace> {
fn capture_backtrace_snapshot() -> Option<Arc<Backtrace>> {
if should_capture_backtrace() {
Some(Backtrace::capture())
Some(Arc::new(Backtrace::capture()))
} else {
None
}
Expand Down Expand Up @@ -321,13 +321,13 @@ impl Error {

#[cfg(feature = "backtrace")]
fn capture_backtrace(&self) -> Option<&CapturedBacktrace> {
if let Some(backtrace) = self.backtrace.as_ref() {
if let Some(backtrace) = self.backtrace.as_deref() {
return Some(backtrace);
}

self.captured_backtrace
.get_or_init(capture_backtrace_snapshot)
.as_ref()
.as_deref()
}

#[cfg(not(feature = "backtrace"))]
Expand All @@ -336,7 +336,7 @@ impl Error {
}

#[cfg(feature = "backtrace")]
fn set_backtrace_slot(&mut self, backtrace: CapturedBacktrace) {
fn set_backtrace_slot(&mut self, backtrace: Arc<Backtrace>) {
self.backtrace = Some(backtrace);
self.captured_backtrace = OnceLock::new();
}
Expand Down Expand Up @@ -574,6 +574,21 @@ impl Error {
/// Attach a captured backtrace.
#[must_use]
pub fn with_backtrace(mut self, backtrace: CapturedBacktrace) -> Self {
#[cfg(feature = "backtrace")]
{
self.set_backtrace_slot(Arc::new(backtrace));
}

#[cfg(not(feature = "backtrace"))]
{
self.set_backtrace_slot(backtrace);
}
self.mark_dirty();
self
}

#[cfg(feature = "backtrace")]
pub(crate) fn with_shared_backtrace(mut self, backtrace: Arc<Backtrace>) -> Self {
self.set_backtrace_slot(backtrace);
self.mark_dirty();
self
Expand Down Expand Up @@ -678,6 +693,18 @@ impl Error {
self.capture_backtrace()
}

#[cfg(feature = "backtrace")]
pub(crate) fn backtrace_shared(&self) -> Option<Arc<Backtrace>> {
if let Some(backtrace) = self.backtrace.as_ref() {
return Some(Arc::clone(backtrace));
}

self.captured_backtrace
.get_or_init(capture_backtrace_snapshot)
.as_ref()
.map(Arc::clone)
}

/// Borrow the source if present.
#[must_use]
pub fn source_ref(&self) -> Option<&(dyn CoreError + Send + Sync + 'static)> {
Expand Down
7 changes: 5 additions & 2 deletions src/result_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ impl<T, E> ResultExt<T, E> for Result<T, E> {
enriched.details = app_err.details.clone();
}
#[cfg(feature = "backtrace")]
if let Some(backtrace) = app_err.backtrace().cloned() {
enriched = enriched.with_backtrace(backtrace);
let shared_backtrace = app_err.backtrace_shared();

#[cfg(feature = "backtrace")]
if let Some(backtrace) = shared_backtrace {
enriched = enriched.with_shared_backtrace(backtrace);
}

enriched.with_context(app_err)
Expand Down
Loading