@@ -115,7 +115,7 @@ impl Error {
115
115
Error :: CreateObfuscatorError ( _) => true ,
116
116
Error :: ObfuscatorError ( _) => true ,
117
117
Error :: PskNegotiationError ( _) => true ,
118
- Error :: TunnelError ( TunnelError :: RecoverableStartWireguardError ) => true ,
118
+ Error :: TunnelError ( TunnelError :: RecoverableStartWireguardError ( .. ) ) => true ,
119
119
120
120
Error :: SetupRoutingError ( error) => error. is_recoverable ( ) ,
121
121
@@ -144,7 +144,7 @@ impl Error {
144
144
pub struct WireguardMonitor {
145
145
runtime : tokio:: runtime:: Handle ,
146
146
/// Tunnel implementation
147
- tunnel : Arc < Mutex < Option < Box < dyn Tunnel > > > > ,
147
+ tunnel : Arc < AsyncMutex < Option < Box < dyn Tunnel > > > > ,
148
148
/// Callback to signal tunnel events
149
149
event_callback : EventCallback ,
150
150
close_msg_receiver : sync_mpsc:: Receiver < CloseMsg > ,
@@ -306,7 +306,7 @@ impl WireguardMonitor {
306
306
let ( pinger_tx, pinger_rx) = sync_mpsc:: channel ( ) ;
307
307
let monitor = WireguardMonitor {
308
308
runtime : args. runtime . clone ( ) ,
309
- tunnel : Arc :: new ( Mutex :: new ( Some ( tunnel) ) ) ,
309
+ tunnel : Arc :: new ( AsyncMutex :: new ( Some ( tunnel) ) ) ,
310
310
event_callback,
311
311
close_msg_receiver : close_obfs_listener,
312
312
pinger_stop_sender : pinger_tx,
@@ -473,7 +473,7 @@ impl WireguardMonitor {
473
473
474
474
#[ allow( clippy:: too_many_arguments) ]
475
475
async fn config_ephemeral_peers < F > (
476
- tunnel : & Arc < Mutex < Option < Box < dyn Tunnel > > > > ,
476
+ tunnel : & Arc < AsyncMutex < Option < Box < dyn Tunnel > > > > ,
477
477
config : & mut Config ,
478
478
retry_attempt : u32 ,
479
479
on_event : F ,
@@ -579,7 +579,7 @@ impl WireguardMonitor {
579
579
#[ cfg( daita) ]
580
580
if config. daita {
581
581
// Start local DAITA machines
582
- let mut tunnel = tunnel. lock ( ) . unwrap ( ) ;
582
+ let mut tunnel = tunnel. lock ( ) . await ;
583
583
if let Some ( tunnel) = tunnel. as_mut ( ) {
584
584
tunnel
585
585
. start_daita ( )
@@ -601,7 +601,7 @@ impl WireguardMonitor {
601
601
/// Reconfigures the tunnel to use the provided config while potentially modifying the config
602
602
/// and restarting the obfuscation provider. Returns the new config used by the new tunnel.
603
603
async fn reconfigure_tunnel (
604
- tunnel : & Arc < Mutex < Option < Box < dyn Tunnel > > > > ,
604
+ tunnel : & Arc < AsyncMutex < Option < Box < dyn Tunnel > > > > ,
605
605
mut config : Config ,
606
606
obfuscator : Arc < AsyncMutex < Option < ObfuscatorHandle > > > ,
607
607
close_obfs_sender : sync_mpsc:: Sender < CloseMsg > ,
@@ -625,11 +625,12 @@ impl WireguardMonitor {
625
625
}
626
626
}
627
627
628
+ let mut tunnel = tunnel. lock ( ) . await ;
629
+
628
630
let set_config_future = tunnel
629
- . lock ( )
630
- . unwrap ( )
631
- . as_ref ( )
631
+ . as_mut ( )
632
632
. map ( |tunnel| tunnel. set_config ( config. clone ( ) ) ) ;
633
+
633
634
if let Some ( f) = set_config_future {
634
635
f. await
635
636
. map_err ( Error :: TunnelError )
@@ -846,8 +847,11 @@ impl WireguardMonitor {
846
847
wait_result
847
848
}
848
849
850
+ /// Tear down the tunnel.
851
+ ///
852
+ /// NOTE: will panic if called from within a tokio runtime.
849
853
fn stop_tunnel ( & mut self ) {
850
- match self . tunnel . lock ( ) . expect ( "Tunnel lock poisoned" ) . take ( ) {
854
+ match self . tunnel . blocking_lock ( ) . take ( ) {
851
855
Some ( tunnel) => {
852
856
if let Err ( e) = tunnel. stop ( ) {
853
857
log:: error!( "{}" , e. display_chain_with_msg( "Failed to stop tunnel" ) ) ;
@@ -1028,10 +1032,10 @@ pub(crate) trait Tunnel: Send {
1028
1032
fn get_interface_name ( & self ) -> String ;
1029
1033
fn stop ( self : Box < Self > ) -> std:: result:: Result < ( ) , TunnelError > ;
1030
1034
fn get_tunnel_stats ( & self ) -> std:: result:: Result < stats:: StatsMap , TunnelError > ;
1031
- fn set_config (
1032
- & self ,
1035
+ fn set_config < ' a > (
1036
+ & ' a mut self ,
1033
1037
_config : Config ,
1034
- ) -> Pin < Box < dyn Future < Output = std:: result:: Result < ( ) , TunnelError > > + Send > > ;
1038
+ ) -> Pin < Box < dyn Future < Output = std:: result:: Result < ( ) , TunnelError > > + Send + ' a > > ;
1035
1039
#[ cfg( daita) ]
1036
1040
/// A [`Tunnel`] capable of using DAITA.
1037
1041
fn start_daita ( & mut self ) -> std:: result:: Result < ( ) , TunnelError > ;
@@ -1045,22 +1049,19 @@ pub enum TunnelError {
1045
1049
/// This is an error returned by the implementation that indicates that trying to establish the
1046
1050
/// tunnel again should work normally. The error encountered is known to be sporadic.
1047
1051
#[ error( "Recoverable error while starting wireguard tunnel" ) ]
1048
- RecoverableStartWireguardError ,
1052
+ RecoverableStartWireguardError ( # [ source ] Box < dyn std :: error :: Error + Send > ) ,
1049
1053
1050
1054
/// An unrecoverable error occurred while starting the wireguard tunnel
1051
1055
///
1052
1056
/// This is an error returned by the implementation that indicates that trying to establish the
1053
1057
/// tunnel again will likely fail with the same error. An error was encountered during tunnel
1054
1058
/// configuration which can't be dealt with gracefully.
1055
1059
#[ error( "Failed to start wireguard tunnel" ) ]
1056
- FatalStartWireguardError ,
1060
+ FatalStartWireguardError ( # [ source ] Box < dyn std :: error :: Error + Send > ) ,
1057
1061
1058
1062
/// Failed to tear down wireguard tunnel.
1059
- #[ error( "Failed to stop wireguard tunnel. Status: {status}" ) ]
1060
- StopWireguardError {
1061
- /// Returned error code
1062
- status : i32 ,
1063
- } ,
1063
+ #[ error( "Failed to tear down wireguard tunnel" ) ]
1064
+ StopWireguardError ( #[ source] Box < dyn std:: error:: Error + Send > ) ,
1064
1065
1065
1066
/// Error whilst trying to parse the WireGuard config to read the stats
1066
1067
#[ error( "Reading tunnel stats failed" ) ]
@@ -1114,8 +1115,8 @@ pub enum TunnelError {
1114
1115
1115
1116
/// Failed to receive DAITA event
1116
1117
#[ cfg( daita) ]
1117
- #[ error( "Failed to receive DAITA event " ) ]
1118
- DaitaReceiveEvent ( i32 ) ,
1118
+ #[ error( "Failed to start DAITA" ) ]
1119
+ StartDaita ( # [ source ] Box < dyn std :: error :: Error + Send > ) ,
1119
1120
1120
1121
/// This tunnel does not support DAITA.
1121
1122
#[ cfg( daita) ]
0 commit comments