Skip to content

Commit 7776ec0

Browse files
committed
Improving error handing according to #33 comments
1 parent e7eab4f commit 7776ec0

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/raw_client.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,17 @@ impl<S: Read + Write> RawClient<S> {
492492

493493
// If the map is not empty, we select a random thread to become the
494494
// new reader thread.
495-
if let Some(sender) = map.values().next() {
496-
sender.send(ChannelMessage::WakeUp)?;
495+
if let Some(err) = map.values().find_map(|sender| {
496+
sender
497+
.send(ChannelMessage::WakeUp)
498+
.map_err(|err| {
499+
warn!("Unable to wake up a thread, trying some other");
500+
err
501+
})
502+
.err()
503+
}) {
504+
error!("All the threads has failed, giving up");
505+
return Err(err)?;
497506
}
498507

499508
break Ok(resp);

src/stream.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use log::error;
12
use std::io::{self, Read, Write};
23
use std::sync::{Arc, Mutex};
34

@@ -8,7 +9,10 @@ impl<T: Read + Write> Read for ClonableStream<T> {
89
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
910
self.0
1011
.lock()
11-
.map_err(|_| io::Error::from(io::ErrorKind::BrokenPipe))?
12+
.map_err(|_| {
13+
error!("Unable to acquire lock on ClonableStream read operation");
14+
io::Error::from(io::ErrorKind::BrokenPipe)
15+
})?
1216
.read(buf)
1317
}
1418
}
@@ -17,14 +21,20 @@ impl<T: Read + Write> Write for ClonableStream<T> {
1721
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
1822
self.0
1923
.lock()
20-
.map_err(|_| io::Error::from(io::ErrorKind::BrokenPipe))?
24+
.map_err(|_| {
25+
error!("Unable to acquire lock on ClonableStream write operation");
26+
io::Error::from(io::ErrorKind::BrokenPipe)
27+
})?
2128
.write(buf)
2229
}
2330

2431
fn flush(&mut self) -> io::Result<()> {
2532
self.0
2633
.lock()
27-
.map_err(|_| io::Error::from(io::ErrorKind::BrokenPipe))?
34+
.map_err(|_| {
35+
error!("Unable to acquire lock on ClonableStream flush operation");
36+
io::Error::from(io::ErrorKind::BrokenPipe)
37+
})?
2838
.flush()
2939
}
3040
}

src/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ impl Display for Error {
344344
Error::MissingDomain => f.write_str("Missing domain while it was explicitly asked to validate it"),
345345
Error::BothSocksAndTimeout => f.write_str("Setting both a proxy and a timeout in `Config` is an error"),
346346
Error::CouldntLockReader => f.write_str("Couldn't take a lock on the reader mutex. This means that there's already another reader thread is running"),
347+
Error::Mpsc => f.write_str("Broken IPC communication channel: the other thread probably has exited"),
347348
}
348349
}
349350
}
@@ -367,7 +368,7 @@ impl_error!(bitcoin::consensus::encode::Error, Bitcoin);
367368

368369
impl<T> From<std::sync::PoisonError<T>> for Error {
369370
fn from(_: std::sync::PoisonError<T>) -> Self {
370-
Error::CouldntLockReader
371+
Error::IOError(std::io::Error::from(std::io::ErrorKind::BrokenPipe))
371372
}
372373
}
373374

0 commit comments

Comments
 (0)