catch the panic to avoid peer thread quit early (#2686)

* catch the panic to avoid peer thread quit before taking the chance to ban
* move catch wrapper logic down into the util crate
* log the panic info
* keep txhashset.rs untouched
* remove a warning
This commit is contained in:
Gary Yu 2019-03-24 04:39:50 +08:00 committed by Ignotus Peverell
parent 148256de6e
commit 32d939189d

View file

@ -15,6 +15,7 @@
use std::fs::{self, File};
/// Wrappers around the `zip-rs` library to compress and decompress zip archives.
use std::io;
use std::panic;
use std::path::Path;
use walkdir::WalkDir;
@ -64,10 +65,19 @@ pub fn compress(src_dir: &Path, dst_file: &File) -> ZipResult<()> {
/// Decompress a source file into the provided destination path.
pub fn decompress<R, F>(src_file: R, dest: &Path, expected: F) -> ZipResult<usize>
where
R: io::Read + io::Seek,
F: Fn(&Path) -> bool,
R: io::Read + io::Seek + panic::UnwindSafe,
F: Fn(&Path) -> bool + panic::UnwindSafe,
{
let mut decompressed = 0;
// catch the panic to avoid the thread quit
panic::set_hook(Box::new(|panic_info| {
error!(
"panic occurred: {:?}",
panic_info.payload().downcast_ref::<&str>().unwrap()
);
}));
let result = panic::catch_unwind(move || {
let mut archive = zip_rs::ZipArchive::new(src_file)?;
for i in 0..archive.len() {
@ -118,4 +128,17 @@ where
}
}
Ok(decompressed)
});
match result {
Ok(res) => match res {
Err(e) => Err(e.into()),
Ok(_) => res,
},
Err(_) => {
error!("panic occurred on zip::decompress!");
Err(zip::result::ZipError::InvalidArchive(
"panic occurred on zip::decompress",
))
}
}
}