Skip to content

[DRAFT] Handle thread join errors uniformly with helper #6266

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

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions stackslib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ mutants = "0.0.3"
rlimit = "0.10.2"
chrono = "0.4.19"
tempfile = "3.3"
proptest = { version = "1.6.0", default-features = false, features = ["std"] }

[features]
default = []
Expand Down
44 changes: 31 additions & 13 deletions stackslib/src/burnchains/burnchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,23 @@ impl BurnchainBlock {
}

impl Burnchain {
pub fn handle_thread_join<T>(
handle: std::thread::JoinHandle<Result<T, burnchain_error>>,
name: &str,
) -> Result<T, burnchain_error> {
match handle.join() {
Ok(Ok(val)) => Ok(val),
Ok(Err(e)) => {
warn!("{} thread error: {:?}", name, e);
Err(e)
}
Err(_) => {
error!("{} thread panicked", name);
Err(burnchain_error::ThreadChannelError)
}
}
}

pub fn new(
working_dir: &str,
chain_name: &str,
Expand Down Expand Up @@ -1783,20 +1800,21 @@ impl Burnchain {
}

// join up
let _ = download_thread.join().unwrap();
let _ = parse_thread.join().unwrap();
let block_header = match db_thread.join().unwrap() {
Ok(x) => x,
Err(e) => {
warn!("Failed to join burnchain download thread: {:?}", &e);
if let burnchain_error::CoordinatorClosed = e {
return Err(burnchain_error::CoordinatorClosed);
} else {
return Err(burnchain_error::TrySyncAgain);
}
}
};
let download_result = Self::handle_thread_join(download_thread, "download");
let parse_result = Self::handle_thread_join(parse_thread, "parse");
let db_result = Self::handle_thread_join(db_thread, "db");

if let Err(e) = download_result {
warn!("Download thread failed: {:?}", e);
return Err(e);
}

if let Err(e) = parse_result {
warn!("Parse thread failed: {:?}", e);
return Err(e);
}

let block_header = db_result?;
if block_header.block_height < end_block {
warn!(
"Try synchronizing the burn chain again: final snapshot {} < {}",
Expand Down
1 change: 1 addition & 0 deletions stackslib/src/burnchains/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
pub mod affirmation;
pub mod burnchain;
pub mod db;
pub mod thread_join;

use std::collections::HashMap;

Expand Down
98 changes: 98 additions & 0 deletions stackslib/src/burnchains/tests/thread_join.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright (C) 2025 Stacks Open Internet Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::time::Duration;
use std::{panic, thread};

use proptest::prelude::*;

use crate::burnchains::bitcoin::Error as bitcoin_error;
use crate::burnchains::{Burnchain, Error as burnchain_error};

#[test]
fn join_success() {
proptest!(|(v in any::<u32>())| {
let h = thread::spawn(move || Ok(v));
let r = Burnchain::handle_thread_join::<u32>(h, "test");
prop_assert!(r.is_ok());
prop_assert_eq!(r.unwrap(), v);
});
}

#[test]
fn join_with_name() {
proptest!(|(s in "[a-zA-Z0-9_-]{1,20}")| {
let h = thread::spawn(|| Ok(42));
let r = Burnchain::handle_thread_join::<u32>(h, &s);
prop_assert!(r.is_ok());
prop_assert_eq!(r.unwrap(), 42);
});
}

#[test]
fn join_download_error() {
proptest!(|(x in any::<u32>())| {
let h = thread::spawn(move || {
let _ = x;
Err(burnchain_error::DownloadError(
bitcoin_error::ConnectionError))
});
let r = Burnchain::handle_thread_join::<u32>(h, "test");
prop_assert!(r.is_err());
match r {
Err(burnchain_error::DownloadError(_)) => {}
_ => return Err(TestCaseError::fail("Expected DownloadError")),
}
});
}

#[test]
fn join_parse_error() {
let h = thread::spawn(move || Err(burnchain_error::ParseError));
let r = Burnchain::handle_thread_join::<u32>(h, "test");
assert!(r.is_err());
match r {
Err(burnchain_error::ParseError) => {}
_ => panic!("Expected ParseError"),
}
}

#[test]
fn join_delay() {
proptest!(|(d in 10u64..100, v in any::<u32>())| {
let h = thread::spawn(move || {
thread::sleep(Duration::from_millis(d));
Ok(v)
});
let r = Burnchain::handle_thread_join::<u32>(h, "test");
prop_assert!(r.is_ok());
prop_assert_eq!(r.unwrap(), v);
});
}

#[test]
fn join_panics() {
let h = thread::spawn(|| {
panic!("boom");
#[allow(unreachable_code)]
Ok(42)
});
let r = Burnchain::handle_thread_join::<u32>(h, "test");
assert!(r.is_err());
match r {
Err(burnchain_error::ThreadChannelError) => {}
_ => panic!("Expected ThreadChannelError"),
}
}
Loading