Skip to content

Commit b90ce2a

Browse files
committed
Davids refactor
1 parent 479214f commit b90ce2a

File tree

2 files changed

+49
-54
lines changed

2 files changed

+49
-54
lines changed

mullvad-daemon/src/lib.rs

+48-54
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use api::AccessMethodEvent;
3333
use device::{AccountEvent, PrivateAccountAndDevice, PrivateDeviceEvent};
3434
use futures::{
3535
channel::{mpsc, oneshot},
36-
future::{abortable, AbortHandle, Future, LocalBoxFuture},
36+
future::{abortable, AbortHandle, Future},
3737
StreamExt,
3838
};
3939
use geoip::GeoIpHandler;
@@ -569,7 +569,6 @@ pub trait EventListener {
569569
pub struct Daemon<L: EventListener> {
570570
tunnel_state: TunnelState,
571571
target_state: PersistentTargetState,
572-
shutting_down: bool,
573572
#[cfg(target_os = "linux")]
574573
exclude_pids: split_tunnel::PidManager,
575574
rx: mpsc::UnboundedReceiver<InternalDaemonEvent>,
@@ -825,7 +824,6 @@ where
825824
locked_down: settings.block_when_disconnected,
826825
},
827826
target_state,
828-
shutting_down: false,
829827
#[cfg(target_os = "linux")]
830828
exclude_pids: split_tunnel::PidManager::new().map_err(Error::InitSplitTunneling)?,
831829
rx: internal_event_rx,
@@ -872,8 +870,7 @@ where
872870
}
873871

874872
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 {
877874
break;
878875
}
879876
}
@@ -883,8 +880,27 @@ where
883880
}
884881

885882
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+
};
888904
for future in shutdown_tasks {
889905
future.await;
890906
}
@@ -902,45 +918,18 @@ where
902918
}
903919
}
904920

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 {
937922
use self::InternalDaemonEvent::*;
923+
let mut should_stop = false;
938924
match event {
939925
TunnelStateTransition(transition) => {
940926
self.handle_tunnel_state_transition(transition).await
941927
}
942928
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+
}
944933
NewAppVersionInfo(app_version_info) => {
945934
self.handle_new_app_version_info(app_version_info);
946935
}
@@ -954,6 +943,7 @@ where
954943
#[cfg(any(windows, target_os = "android", target_os = "macos"))]
955944
ExcludedPathsEvent(update, tx) => self.handle_new_excluded_paths(update, tx).await,
956945
}
946+
should_stop
957947
}
958948

959949
async fn handle_tunnel_state_transition(
@@ -1138,11 +1128,6 @@ where
11381128

11391129
async fn handle_command(&mut self, command: DaemonCommand) {
11401130
use self::DaemonCommand::*;
1141-
if self.shutting_down {
1142-
log::trace!("Dropping daemon command because the daemon is shutting down",);
1143-
return;
1144-
}
1145-
11461131
if self.tunnel_state.is_disconnected() {
11471132
self.api_handle.availability.reset_inactivity_timer();
11481133
}
@@ -1417,12 +1402,8 @@ where
14171402
tx: oneshot::Sender<bool>,
14181403
new_target_state: TargetState,
14191404
) {
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");
14261407
}
14271408

14281409
fn on_reconnect(&mut self, tx: oneshot::Sender<bool>) {
@@ -1707,7 +1688,7 @@ where
17071688
}
17081689

17091690
// Shut the daemon down.
1710-
self.trigger_shutdown_event(false);
1691+
let _ = self.tx.send(InternalDaemonEvent::TriggerShutdown(false));
17111692

17121693
self.shutdown_tasks.push(Box::pin(async move {
17131694
if let Err(e) = cleanup::clear_directories().await {
@@ -2636,7 +2617,7 @@ where
26362617
}
26372618
}
26382619

2639-
fn trigger_shutdown_event(&mut self, user_init_shutdown: bool) {
2620+
async fn shutdown(&mut self, user_init_shutdown: bool) {
26402621
// Block all traffic before shutting down to ensure that no traffic can leak on boot or
26412622
// shutdown.
26422623
if !user_init_shutdown
@@ -2647,8 +2628,21 @@ where
26472628
self.send_tunnel_command(TunnelCommand::BlockWhenDisconnected(true, tx));
26482629
}
26492630

2650-
self.shutting_down = true;
26512631
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+
}
26522646
}
26532647

26542648
fn on_prepare_restart(&mut self) {

wireguard-go-rs/libwg/wireguard-go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit c14351e8550c8110df236a2a4354599747552c15

0 commit comments

Comments
 (0)