@@ -33,7 +33,7 @@ use api::AccessMethodEvent;
33
33
use device:: { AccountEvent , PrivateAccountAndDevice , PrivateDeviceEvent } ;
34
34
use futures:: {
35
35
channel:: { mpsc, oneshot} ,
36
- future:: { abortable, AbortHandle , Future , LocalBoxFuture } ,
36
+ future:: { abortable, AbortHandle , Future } ,
37
37
StreamExt ,
38
38
} ;
39
39
use geoip:: GeoIpHandler ;
@@ -569,7 +569,6 @@ pub trait EventListener {
569
569
pub struct Daemon < L : EventListener > {
570
570
tunnel_state : TunnelState ,
571
571
target_state : PersistentTargetState ,
572
- shutting_down : bool ,
573
572
#[ cfg( target_os = "linux" ) ]
574
573
exclude_pids : split_tunnel:: PidManager ,
575
574
rx : mpsc:: UnboundedReceiver < InternalDaemonEvent > ,
@@ -825,7 +824,6 @@ where
825
824
locked_down : settings. block_when_disconnected ,
826
825
} ,
827
826
target_state,
828
- shutting_down : false ,
829
827
#[ cfg( target_os = "linux" ) ]
830
828
exclude_pids : split_tunnel:: PidManager :: new ( ) . map_err ( Error :: InitSplitTunneling ) ?,
831
829
rx : internal_event_rx,
@@ -872,8 +870,7 @@ where
872
870
}
873
871
874
872
while let Some ( event) = self . rx . next ( ) . await {
875
- self . handle_event ( event) . await ;
876
- if self . shutting_down && self . tunnel_state . is_disconnected ( ) {
873
+ if self . handle_event ( event) . await {
877
874
break ;
878
875
}
879
876
}
@@ -883,8 +880,27 @@ where
883
880
}
884
881
885
882
async fn finalize ( self ) {
886
- let ( event_listener, shutdown_tasks, api_runtime, tunnel_state_machine_handle) =
887
- self . shutdown ( ) ;
883
+ let ( event_listener, shutdown_tasks, api_runtime, tunnel_state_machine_handle) = {
884
+ let Daemon {
885
+ event_listener,
886
+ mut shutdown_tasks,
887
+ api_runtime,
888
+ tunnel_state_machine_handle,
889
+ target_state,
890
+ account_manager,
891
+ ..
892
+ } = self ;
893
+
894
+ shutdown_tasks. push ( Box :: pin ( target_state. finalize ( ) ) ) ;
895
+ shutdown_tasks. push ( Box :: pin ( account_manager. shutdown ( ) ) ) ;
896
+
897
+ (
898
+ event_listener,
899
+ shutdown_tasks,
900
+ api_runtime,
901
+ tunnel_state_machine_handle,
902
+ )
903
+ } ;
888
904
for future in shutdown_tasks {
889
905
future. await ;
890
906
}
@@ -902,45 +918,18 @@ where
902
918
}
903
919
}
904
920
905
- /// Shuts down the daemon without shutting down the underlying event listener and the shutdown
906
- /// callbacks
907
- fn shutdown < ' a > (
908
- self ,
909
- ) -> (
910
- L ,
911
- Vec < LocalBoxFuture < ' a , ( ) > > ,
912
- mullvad_api:: Runtime ,
913
- TunnelStateMachineHandle ,
914
- ) {
915
- let Daemon {
916
- event_listener,
917
- mut shutdown_tasks,
918
- api_runtime,
919
- tunnel_state_machine_handle,
920
- target_state,
921
- account_manager,
922
- ..
923
- } = self ;
924
-
925
- shutdown_tasks. push ( Box :: pin ( target_state. finalize ( ) ) ) ;
926
- shutdown_tasks. push ( Box :: pin ( account_manager. shutdown ( ) ) ) ;
927
-
928
- (
929
- event_listener,
930
- shutdown_tasks,
931
- api_runtime,
932
- tunnel_state_machine_handle,
933
- )
934
- }
935
-
936
- async fn handle_event ( & mut self , event : InternalDaemonEvent ) {
921
+ async fn handle_event ( & mut self , event : InternalDaemonEvent ) -> bool {
937
922
use self :: InternalDaemonEvent :: * ;
923
+ let mut should_stop = false ;
938
924
match event {
939
925
TunnelStateTransition ( transition) => {
940
926
self . handle_tunnel_state_transition ( transition) . await
941
927
}
942
928
Command ( command) => self . handle_command ( command) . await ,
943
- TriggerShutdown ( user_init_shutdown) => self . trigger_shutdown_event ( user_init_shutdown) ,
929
+ TriggerShutdown ( user_init_shutdown) => {
930
+ self . shutdown ( user_init_shutdown) . await ;
931
+ should_stop = true ;
932
+ }
944
933
NewAppVersionInfo ( app_version_info) => {
945
934
self . handle_new_app_version_info ( app_version_info) ;
946
935
}
@@ -954,6 +943,7 @@ where
954
943
#[ cfg( any( windows, target_os = "android" , target_os = "macos" ) ) ]
955
944
ExcludedPathsEvent ( update, tx) => self . handle_new_excluded_paths ( update, tx) . await ,
956
945
}
946
+ should_stop
957
947
}
958
948
959
949
async fn handle_tunnel_state_transition (
@@ -1138,11 +1128,6 @@ where
1138
1128
1139
1129
async fn handle_command ( & mut self , command : DaemonCommand ) {
1140
1130
use self :: DaemonCommand :: * ;
1141
- if self . shutting_down {
1142
- log:: trace!( "Dropping daemon command because the daemon is shutting down" , ) ;
1143
- return ;
1144
- }
1145
-
1146
1131
if self . tunnel_state . is_disconnected ( ) {
1147
1132
self . api_handle . availability . reset_inactivity_timer ( ) ;
1148
1133
}
@@ -1417,12 +1402,8 @@ where
1417
1402
tx : oneshot:: Sender < bool > ,
1418
1403
new_target_state : TargetState ,
1419
1404
) {
1420
- if !self . shutting_down {
1421
- let state_change_initated = self . set_target_state ( new_target_state) . await ;
1422
- Self :: oneshot_send ( tx, state_change_initated, "state change initiated" ) ;
1423
- } else {
1424
- log:: warn!( "Ignoring target state change request due to shutdown" ) ;
1425
- }
1405
+ let state_change_initated = self . set_target_state ( new_target_state) . await ;
1406
+ Self :: oneshot_send ( tx, state_change_initated, "state change initiated" ) ;
1426
1407
}
1427
1408
1428
1409
fn on_reconnect ( & mut self , tx : oneshot:: Sender < bool > ) {
@@ -1707,7 +1688,7 @@ where
1707
1688
}
1708
1689
1709
1690
// Shut the daemon down.
1710
- self . trigger_shutdown_event ( false ) ;
1691
+ let _ = self . tx . send ( InternalDaemonEvent :: TriggerShutdown ( false ) ) ;
1711
1692
1712
1693
self . shutdown_tasks . push ( Box :: pin ( async move {
1713
1694
if let Err ( e) = cleanup:: clear_directories ( ) . await {
@@ -2636,7 +2617,7 @@ where
2636
2617
}
2637
2618
}
2638
2619
2639
- fn trigger_shutdown_event ( & mut self , user_init_shutdown : bool ) {
2620
+ async fn shutdown ( & mut self , user_init_shutdown : bool ) {
2640
2621
// Block all traffic before shutting down to ensure that no traffic can leak on boot or
2641
2622
// shutdown.
2642
2623
if !user_init_shutdown
@@ -2647,8 +2628,21 @@ where
2647
2628
self . send_tunnel_command ( TunnelCommand :: BlockWhenDisconnected ( true , tx) ) ;
2648
2629
}
2649
2630
2650
- self . shutting_down = true ;
2651
2631
self . disconnect_tunnel ( ) ;
2632
+
2633
+ if !self . tunnel_state . is_disconnected ( ) {
2634
+ while let Some ( event) = self . rx . next ( ) . await {
2635
+ if let InternalDaemonEvent :: TunnelStateTransition ( transition) = event {
2636
+ self . handle_tunnel_state_transition ( transition) . await ;
2637
+ } else {
2638
+ log:: trace!( "Ignoring event because the daemon is shutting down" ) ;
2639
+ }
2640
+
2641
+ if self . tunnel_state . is_disconnected ( ) {
2642
+ break ;
2643
+ }
2644
+ }
2645
+ }
2652
2646
}
2653
2647
2654
2648
fn on_prepare_restart ( & mut self ) {
0 commit comments