Skip to content

Commit 7998a58

Browse files
committed
refactor: log error on thread exit
1 parent 3467ad8 commit 7998a58

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

src/client.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ use byteorder::{BigEndian, WriteBytesExt};
99
use monoio::{
1010
buf::IoBufMut,
1111
io::{AsyncReadRent, AsyncReadRentExt, AsyncWriteRent, AsyncWriteRentExt, Splitable},
12-
net::{TcpListener, TcpStream},
12+
net::TcpStream,
1313
};
1414
use monoio_rustls_fork_shadow_tls::TlsConnector;
1515
use rand::{prelude::Distribution, seq::SliceRandom, Rng};
1616
use rustls_fork_shadow_tls::{OwnedTrustAnchor, RootCertStore, ServerName};
1717

1818
use crate::{
1919
helper_v2::{copy_with_application_data, copy_without_application_data, HashedReadStream},
20-
util::{kdf, mod_tcp_conn, prelude::*, verified_relay, xor_slice, Hmac, V3Mode},
20+
util::{
21+
bind_with_pretty_error, kdf, mod_tcp_conn, prelude::*, verified_relay, xor_slice, Hmac,
22+
V3Mode,
23+
},
2124
};
2225

2326
const FAKE_REQUEST_LENGTH_RANGE: (usize, usize) = (16, 64);
@@ -156,8 +159,7 @@ impl<LA, TA> ShadowTlsClient<LA, TA> {
156159
LA: std::net::ToSocketAddrs + 'static,
157160
TA: std::net::ToSocketAddrs + 'static,
158161
{
159-
let listener = TcpListener::bind(self.listen_addr.as_ref())
160-
.map_err(|e| anyhow::anyhow!("bind failed, check if the port is used: {e}"))?;
162+
let listener = bind_with_pretty_error(self.listen_addr.as_ref())?;
161163
let shared = Rc::new(self);
162164
loop {
163165
match listener.accept().await {

src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,17 @@ fn main() {
270270
.enable_timer()
271271
.build()
272272
.expect("unable to build monoio runtime(please refer to: https://github.com/ihciah/shadow-tls/wiki/How-to-Run#common-issues)");
273-
let _ = rt.block_on(runnable_clone.serve());
273+
rt.block_on(runnable_clone.serve())
274274
});
275275
threads.push(t);
276276
}
277277
if let Err(e) = ctrlc::set_handler(|| std::process::exit(0)) {
278278
tracing::error!("Unable to register signal handler: {e}");
279279
}
280280
threads.into_iter().for_each(|t| {
281-
let _ = t.join();
281+
if let Err(e) = t.join().expect("couldn't join on the associated thread") {
282+
tracing::error!("Thread exit: {e}");
283+
}
282284
});
283285
}
284286

src/server.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use monoio::{
1616
AsyncReadRent, AsyncReadRentExt, AsyncWriteRent, AsyncWriteRentExt, PrefixedReadIo,
1717
Splitable,
1818
},
19-
net::{TcpListener, TcpStream},
19+
net::TcpStream,
2020
};
2121

2222
use crate::{
@@ -25,8 +25,8 @@ use crate::{
2525
FutureOrOutput, HashedWriteStream, HmacHandler, HMAC_SIZE_V2,
2626
},
2727
util::{
28-
copy_bidirectional, copy_until_eof, kdf, mod_tcp_conn, prelude::*, verified_relay,
29-
xor_slice, Hmac, V3Mode,
28+
bind_with_pretty_error, copy_bidirectional, copy_until_eof, kdf, mod_tcp_conn, prelude::*,
29+
verified_relay, xor_slice, Hmac, V3Mode,
3030
},
3131
};
3232

@@ -148,8 +148,7 @@ impl<LA, TA> ShadowTlsServer<LA, TA> {
148148
LA: std::net::ToSocketAddrs + 'static,
149149
TA: std::net::ToSocketAddrs + 'static,
150150
{
151-
let listener = TcpListener::bind(self.listen_addr.as_ref())
152-
.map_err(|e| anyhow::anyhow!("bind failed, check if the port is used: {e}"))?;
151+
let listener = bind_with_pretty_error(self.listen_addr.as_ref())?;
153152
let shared = Rc::new(self);
154153
loop {
155154
match listener.accept().await {

src/util.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::{ptr::copy_nonoverlapping, time::Duration};
1+
use std::{io::ErrorKind, net::ToSocketAddrs, ptr::copy_nonoverlapping, time::Duration};
22

33
use byteorder::{BigEndian, WriteBytesExt};
44
use local_sync::oneshot::{Receiver, Sender};
55
use monoio::{
66
buf::IoBufMut,
77
io::{AsyncReadRent, AsyncWriteRent, AsyncWriteRentExt, Splitable},
8-
net::TcpStream,
8+
net::{TcpListener, TcpStream},
99
};
1010

1111
use hmac::Mac;
@@ -176,6 +176,19 @@ pub async fn verified_relay(
176176
);
177177
}
178178

179+
/// Bind with pretty error.
180+
pub fn bind_with_pretty_error<A: ToSocketAddrs>(addr: A) -> anyhow::Result<TcpListener> {
181+
TcpListener::bind(addr).map_err(|e| match e.kind() {
182+
ErrorKind::AddrInUse => {
183+
anyhow::anyhow!("bind failed, check if the port is used: {e}")
184+
}
185+
ErrorKind::PermissionDenied => {
186+
anyhow::anyhow!("bind failed, check if permission configured correct: {e}")
187+
}
188+
_ => anyhow::anyhow!("bind failed: {e}"),
189+
})
190+
}
191+
179192
/// Remove application data header, verify hmac, remove the
180193
/// hmac header and copy.
181194
async fn copy_remove_appdata_and_verify(

0 commit comments

Comments
 (0)