Skip to content

Commit 0db0728

Browse files
authored
Merge pull request #31 from kryvashek/avoid-thread-kill-via-panic
Avoid thread kill via panic Sorry this took so long! This bumps the version to 2.8.0
2 parents c0c101f + 2d261b1 commit 0db0728

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## 2.8.0 - 2023-06-16
8+
9+
* Call of deprecated `err.description()` replaced with `err.to_string()`.
10+
* Avoided all catchable panics in async drain thread.
11+
712
## 2.7.0 - 2021-07-29
813

914
* Fix license field to be a valid SPDX expression

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "slog-async"
3-
version = "2.7.0"
3+
version = "2.8.0"
44
authors = ["Dawid Ciężarkiewicz <dpc@dpc.pw>"]
55
description = "Asynchronous drain for slog-rs"
66
keywords = ["slog", "logging", "log", "asynchronous"]

lib.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ use slog::{BorrowedKV, Level, Record, RecordStatic, SingleKV, KV};
6262
use slog::{Key, OwnedKVList, Serializer};
6363

6464
use slog::Drain;
65-
use std::error::Error;
6665
use std::fmt;
6766
use std::sync;
6867
use std::{io, thread};
@@ -71,6 +70,8 @@ use std::sync::atomic::AtomicUsize;
7170
use std::sync::atomic::Ordering;
7271
use std::sync::Mutex;
7372
use take_mut::take;
73+
74+
use std::panic::{catch_unwind, AssertUnwindSafe};
7475
// }}}
7576

7677
// {{{ Serializer
@@ -222,7 +223,7 @@ impl<T> From<std::sync::PoisonError<T>> for AsyncError {
222223
fn from(err: std::sync::PoisonError<T>) -> AsyncError {
223224
AsyncError::Fatal(Box::new(io::Error::new(
224225
io::ErrorKind::BrokenPipe,
225-
err.description(),
226+
err.to_string(),
226227
)))
227228
}
228229
}
@@ -294,12 +295,25 @@ where
294295
}
295296
let drain = self.drain;
296297
let join = builder
297-
.spawn(move || loop {
298-
match rx.recv().unwrap() {
299-
AsyncMsg::Record(r) => {
300-
r.log_to(&drain).unwrap();
298+
.spawn(move || {
299+
let drain = AssertUnwindSafe(&drain);
300+
// catching possible unwinding panics which can occur in used inner Drain implementation
301+
if let Err(panic_cause) = catch_unwind(move || loop {
302+
match rx.recv() {
303+
Ok(AsyncMsg::Record(r)) => {
304+
if r.log_to(&*drain).is_err() {
305+
eprintln!("slog-async failed while writing");
306+
return;
307+
}
308+
}
309+
Ok(AsyncMsg::Finish) => return,
310+
Err(recv_error) => {
311+
eprintln!("slog-async failed while receiving: {recv_error}");
312+
return;
313+
}
301314
}
302-
AsyncMsg::Finish => return,
315+
}) {
316+
eprintln!("slog-async failed with panic: {panic_cause:?}")
303317
}
304318
})
305319
.unwrap();

0 commit comments

Comments
 (0)