diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index 3566347152b4..4de3beb26a6a 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -1,9 +1,7 @@ use mullvad_daemon::{ logging, management_interface::{ManagementInterfaceEventBroadcaster, ManagementInterfaceServer}, - rpc_uniqueness_check, - runtime::new_runtime_builder, - version, Daemon, DaemonCommandChannel, DaemonCommandSender, + rpc_uniqueness_check, runtime, version, Daemon, DaemonCommandChannel, DaemonCommandSender, }; use std::{path::PathBuf, thread, time::Duration}; use talpid_types::ErrorExt; @@ -22,11 +20,7 @@ const DAEMON_LOG_FILENAME: &str = "daemon.log"; const EARLY_BOOT_LOG_FILENAME: &str = "early-boot-fw.log"; fn main() { - let runtime = new_runtime_builder().build().unwrap_or_else(|e| { - eprintln!("{}", e.display_chain()); - std::process::exit(1); - }); - + let runtime = new_runtime(); let exit_code = match runtime.block_on(run()) { Ok(_) => 0, Err(error) => { @@ -44,6 +38,19 @@ fn main() { std::process::exit(exit_code); } +fn new_runtime() -> tokio::runtime::Runtime { + let mut builder = match cli::get_config().command { + #[cfg(target_os = "windows")] + cli::Command::RunAsService | cli::Command::RegisterService => runtime::new_current_thread(), + _ => runtime::new_multi_thread(), + }; + + builder.build().unwrap_or_else(|e| { + eprintln!("{}", e.display_chain()); + std::process::exit(1); + }) +} + async fn run() -> Result<(), String> { let config = cli::get_config(); @@ -69,8 +76,8 @@ async fn run() -> Result<(), String> { #[cfg(target_os = "windows")] cli::Command::RunAsService => { - init_logger(config, None)?; assert_unique().await?; + let _ = init_daemon_logging(config)?; system_service::run() } diff --git a/mullvad-daemon/src/runtime.rs b/mullvad-daemon/src/runtime.rs index 34bdf60390bc..86d251db4c0c 100644 --- a/mullvad-daemon/src/runtime.rs +++ b/mullvad-daemon/src/runtime.rs @@ -1,7 +1,13 @@ use tokio::runtime; -pub fn new_runtime_builder() -> runtime::Builder { +pub fn new_multi_thread() -> runtime::Builder { let mut builder = runtime::Builder::new_multi_thread(); builder.worker_threads(4).enable_all(); builder } + +pub fn new_current_thread() -> runtime::Builder { + let mut builder = runtime::Builder::new_current_thread(); + builder.enable_all(); + builder +} diff --git a/mullvad-daemon/src/system_service.rs b/mullvad-daemon/src/system_service.rs index 21b881d1e031..a5a76e72b440 100644 --- a/mullvad-daemon/src/system_service.rs +++ b/mullvad-daemon/src/system_service.rs @@ -1,5 +1,5 @@ use crate::cli; -use mullvad_daemon::{runtime::new_runtime_builder, DaemonShutdownHandle}; +use mullvad_daemon::{runtime::new_multi_thread, DaemonShutdownHandle}; use once_cell::sync::Lazy; use std::{ env, @@ -98,7 +98,7 @@ pub fn handle_service_main(_arguments: Vec) { let log_dir = crate::get_log_dir(cli::get_config()).expect("Log dir should be available here"); - let runtime = new_runtime_builder().build(); + let runtime = new_multi_thread().build(); let runtime = match runtime { Err(error) => { log::error!("{}", error.display_chain()); diff --git a/mullvad-jni/src/lib.rs b/mullvad-jni/src/lib.rs index 9264b5e8957b..64c9465313df 100644 --- a/mullvad-jni/src/lib.rs +++ b/mullvad-jni/src/lib.rs @@ -19,7 +19,7 @@ use jnix::{ }; use mullvad_api::{rest::Error as RestError, StatusCode}; use mullvad_daemon::{ - device, exception_logging, logging, runtime::new_runtime_builder, + device, exception_logging, logging, runtime::new_multi_thread, settings::patch::Error as PatchError, version, Daemon, DaemonCommandChannel, }; use mullvad_types::{ @@ -551,7 +551,7 @@ fn spawn_daemon( .map_err(Error::CreateGlobalReference)?; let (tx, rx) = mpsc::channel(); - let runtime = new_runtime_builder() + let runtime = new_multi_thread() .build() .map_err(Error::InitializeTokioRuntime)?;