Skip to content

Commit

Permalink
Start tokio runtime after daemonize
Browse files Browse the repository at this point in the history
  • Loading branch information
Saruniks committed Aug 8, 2024
1 parent 82f4f1b commit cf5f369
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ dist-client = [
]
# Enables the sccache-dist binary
dist-server = [
"reqwest/blocking",
"jwt",
"flate2",
"libmount",
Expand Down
17 changes: 10 additions & 7 deletions src/bin/sccache-dist/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use std::path::Path;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::{Duration, Instant};
use tokio::runtime::Runtime;

#[cfg_attr(target_os = "freebsd", path = "build_freebsd.rs")]
mod build;
Expand All @@ -40,8 +41,7 @@ pub const INSECURE_DIST_SERVER_TOKEN: &str = "dangerously_insecure_server";
all(target_os = "linux", target_arch = "x86_64"),
target_os = "freebsd"
))]
#[tokio::main]
async fn main() {
fn main() {
init_logging();

let incr_env_strs = ["CARGO_BUILD_INCREMENTAL", "CARGO_INCREMENTAL"];
Expand Down Expand Up @@ -69,7 +69,7 @@ async fn main() {
},
};

std::process::exit(match run(command).await {
std::process::exit(match run(command) {
Ok(s) => s,
Err(e) => {
eprintln!("sccache-dist: error: {}", e);
Expand Down Expand Up @@ -132,7 +132,7 @@ fn check_jwt_server_token(
.ok()
}

async fn run(command: Command) -> Result<i32> {
fn run(command: Command) -> Result<i32> {
match command {
Command::Auth(AuthSubcommand::Base64 { num_bytes }) => {
let mut bytes = vec![0; num_bytes];
Expand Down Expand Up @@ -171,7 +171,6 @@ async fn run(command: Command) -> Result<i32> {
jwks_url,
} => Box::new(
token_check::ValidJWTCheck::new(audience, issuer, &jwks_url)
.await
.context("Failed to create a checker for valid JWTs")?,
),
scheduler_config::ClientAuth::Mozilla { required_groups } => {
Expand Down Expand Up @@ -219,7 +218,10 @@ async fn run(command: Command) -> Result<i32> {
check_client_auth,
check_server_auth,
);
http_scheduler.start().await?;

// Create runtime after daemonize because Tokio doesn't work well with daemonize
let runtime = Runtime::new().context("Failed to create Tokio runtime")?;
runtime.block_on(async { http_scheduler.start().await })?;
unreachable!();
}

Expand Down Expand Up @@ -296,7 +298,8 @@ async fn run(command: Command) -> Result<i32> {
server,
)
.context("Failed to create sccache HTTP server instance")?;
http_server.start().await?;
let runtime = Runtime::new().context("Failed to create Tokio runtime")?;
runtime.block_on(async { http_server.start().await })?;
unreachable!();
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/bin/sccache-dist/token_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,12 @@ impl ClientAuthCheck for ValidJWTCheck {
}

impl ValidJWTCheck {
pub async fn new(audience: String, issuer: String, jwks_url: &str) -> Result<Self> {
let res = reqwest::get(jwks_url)
.await
.context("Failed to make request to JWKs url")?;
pub fn new(audience: String, issuer: String, jwks_url: &str) -> Result<Self> {
let res = reqwest::blocking::get(jwks_url).context("Failed to make request to JWKs url")?;
if !res.status().is_success() {
bail!("Could not retrieve JWKs, HTTP error: {}", res.status())
}
let jwks: Jwks = res.json().await.context("Failed to parse JWKs json")?;
let jwks: Jwks = res.json().context("Failed to parse JWKs json")?;
let kid_to_pkcs1 = jwks
.keys
.into_iter()
Expand Down

0 comments on commit cf5f369

Please sign in to comment.