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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## 2.8.0 - 2023-06-16

* Call of deprecated `err.description()` replaced with `err.to_string()`.
* Avoided all catchable panics in async drain thread.

## 2.7.0 - 2021-07-29

* Fix license field to be a valid SPDX expression
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "slog-async"
version = "2.7.0"
version = "2.8.0"
authors = ["Dawid Ciężarkiewicz <dpc@dpc.pw>"]
description = "Asynchronous drain for slog-rs"
keywords = ["slog", "logging", "log", "asynchronous"]
Expand Down
28 changes: 21 additions & 7 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ use slog::{BorrowedKV, Level, Record, RecordStatic, SingleKV, KV};
use slog::{Key, OwnedKVList, Serializer};

use slog::Drain;
use std::error::Error;
use std::fmt;
use std::sync;
use std::{io, thread};
Expand All @@ -71,6 +70,8 @@ use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Mutex;
use take_mut::take;

use std::panic::{catch_unwind, AssertUnwindSafe};
// }}}

// {{{ Serializer
Expand Down Expand Up @@ -222,7 +223,7 @@ impl<T> From<std::sync::PoisonError<T>> for AsyncError {
fn from(err: std::sync::PoisonError<T>) -> AsyncError {
AsyncError::Fatal(Box::new(io::Error::new(
io::ErrorKind::BrokenPipe,
err.description(),
err.to_string(),
)))
}
}
Expand Down Expand Up @@ -294,12 +295,25 @@ where
}
let drain = self.drain;
let join = builder
.spawn(move || loop {
match rx.recv().unwrap() {
AsyncMsg::Record(r) => {
r.log_to(&drain).unwrap();
.spawn(move || {
let drain = AssertUnwindSafe(&drain);
// catching possible unwinding panics which can occur in used inner Drain implementation
if let Err(panic_cause) = catch_unwind(move || loop {
match rx.recv() {
Ok(AsyncMsg::Record(r)) => {
if r.log_to(&*drain).is_err() {
eprintln!("slog-async failed while writing");
return;
}
}
Ok(AsyncMsg::Finish) => return,
Err(recv_error) => {
eprintln!("slog-async failed while receiving: {recv_error}");
return;
}
}
AsyncMsg::Finish => return,
}) {
eprintln!("slog-async failed with panic: {panic_cause:?}")
}
})
.unwrap();
Expand Down