fn check(&mut self, join: bool) -> anyhow::Result<()> {
        let err_arc = match self {
            Self::Handle(handle) if join || handle.is_finished() => {
                let Self::Handle(handle) = mem::take(self) else {
                    unreachable!("just checked variant earlier");
                };
                let err = match handle.join() {
                    Err(_) => anyhow::anyhow!("persistence thread panicked"),
                    // Handling normal exits depends on whether we expect the thread to exit.
                    Ok(Ok(())) if join => return Ok(()),
                    Ok(Ok(())) => anyhow::anyhow!("persistence thread unexpectedly stopped"),
                    Ok(Err(err)) => err,
                };
                let err: Box<dyn StdError + Send + Sync> = err.into();
                let err: Arc<dyn StdError + Send + Sync> = err.into();
                *self = Self::Err(err.clone());
                err
            }
            Self::Handle(_) => return Ok(()),
            Self::Err(err) => err.clone(),
            Self::Nothing => unreachable!("only used temporarily to take out `JoinHandle`"),
        };
        Err(anyhow::Error::new(err_arc))
    }
Checks whether the thread handle has exited, and returns an error if it exited with an error.
If `join` is set, waits for the thread handle to exit.
core/lib/merkle_tree/src/storage/parallel.rs