Skip to content

Commit

Permalink
Pass Arc<io::Error> to waiting threads
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Nov 24, 2020
1 parent f200b7f commit 005f4cb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/raw_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::mem::drop;
use std::net::{SocketAddr, TcpStream, ToSocketAddrs};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::{Mutex, TryLockError};
use std::sync::{Mutex, TryLockError, Arc};
use std::time::Duration;

#[allow(unused_imports)]
Expand Down Expand Up @@ -352,7 +352,7 @@ impl RawClient<ElectrumProxyStream> {
enum ChannelMessage {
Response(serde_json::Value),
WakeUp,
Error,
Error(Arc<std::io::Error>),
}

impl<S: Read + Write> RawClient<S> {
Expand Down Expand Up @@ -398,9 +398,10 @@ impl<S: Read + Write> RawClient<S> {
loop {
raw_resp.clear();

if reader.read_line(&mut raw_resp).is_err() {
if let Err(e) = reader.read_line(&mut raw_resp) {
let error = Arc::new(e);
for (_, s) in self.waiting_map.lock().unwrap().drain() {
s.send(ChannelMessage::Error)
s.send(ChannelMessage::Error(error.clone()))
.expect("Unable to send ChannelMessage::Error");
}
}
Expand Down Expand Up @@ -531,10 +532,10 @@ impl<S: Read + Write> RawClient<S> {

continue;
}
Ok(ChannelMessage::Error) => {
Ok(ChannelMessage::Error(e)) => {
warn!("Received ChannelMessage::Error");

break Err(Error::ChannelError);
break Err(Error::ChannelError(e));
}
e @ Err(_) => e.map(|_| ()).expect("Error receiving from channel"), // panic if there's something wrong with the channels
}
Expand Down
3 changes: 2 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use std::convert::TryFrom;
use std::ops::Deref;
use std::sync::Arc;

use bitcoin::blockdata::block;
use bitcoin::consensus::encode::deserialize;
Expand Down Expand Up @@ -291,7 +292,7 @@ pub enum Error {
/// Made one or multiple attempts, always in Error
AllAttemptsErrored(Vec<Error>),
/// There was an error transmitted from the reader thread to others
ChannelError,
ChannelError(Arc<std::io::Error>),
/// Setting both a proxy and a timeout in `Config` results in this error
BothSocksAndTimeout,
/// Setting both a timeout and passing zero or more than one socket addrs is an error
Expand Down

0 comments on commit 005f4cb

Please sign in to comment.