Skip to content

Commit

Permalink
Use single-threaded runtime when launching service
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Apr 9, 2024
1 parent dde286f commit d0b9c89
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
23 changes: 15 additions & 8 deletions mullvad-daemon/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) => {
Expand All @@ -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();

Expand Down
8 changes: 7 additions & 1 deletion mullvad-daemon/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions mullvad-daemon/src/system_service.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -98,7 +98,7 @@ pub fn handle_service_main(_arguments: Vec<OsString>) {

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());
Expand Down
4 changes: 2 additions & 2 deletions mullvad-jni/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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)?;

Expand Down

0 comments on commit d0b9c89

Please sign in to comment.