From f3d542ba600ab689edc84039cb19c944faebe58c Mon Sep 17 00:00:00 2001 From: Josh Knapp Date: Mon, 16 Nov 2026 10:56:27 -0800 Subject: [PATCH] fix(aws): build the pricing client inside the cost estimate's 8 s bound Co-Authored-By: Claude Opus 5.5 --- app/src-tauri/src/aws/pricing.rs | 25 ++++++++++++++++----- app/src-tauri/src/aws/pricing/tests.rs | 24 ++++++++++++++++++++ app/src-tauri/src/commands/host_commands.rs | 14 ++++++------ 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/app/src-tauri/src/aws/pricing.rs b/app/src-tauri/src/aws/pricing.rs index b3a872d..80dcb3a 100644 --- a/app/src-tauri/src/aws/pricing.rs +++ b/app/src-tauri/src/aws/pricing.rs @@ -347,20 +347,35 @@ impl RatesCache { api: Option<&A>, region: &str, now: SystemTime, + ) -> Option { + self.rates_with::(async { api }, region, now).await + } + + /// [`Self::rates`] with the client built by `api`, which is awaited only + /// when the cache can't answer, and within the same [`FETCH_TIMEOUT`] as + /// the fetch (building one reads credentials, which can hang too). + pub async fn rates_with>( + &self, + api: impl Future>, + region: &str, + now: SystemTime, ) -> Option { let cached = self.read(region, now); if let Some((rates, true)) = cached { return Some(rates); } let stale = cached.map(|(rates, _)| rates); - let Some(api) = api else { - return stale; - }; if self.recently_failed(region, now) { return stale; } - let fetched = match tokio::time::timeout(FETCH_TIMEOUT, fetch_rates(api, region)).await { - Ok(r) => r, + let fetch = async { + let api = api.await?; + Some(fetch_rates(api.borrow(), region).await) + }; + let fetched = match tokio::time::timeout(FETCH_TIMEOUT, fetch).await { + Ok(Some(r)) => r, + // No client could be built: the stale cache answers. + Ok(None) => return stale, Err(_) => Err(format!("timed out after {}s", FETCH_TIMEOUT.as_secs())), }; match fetched { diff --git a/app/src-tauri/src/aws/pricing/tests.rs b/app/src-tauri/src/aws/pricing/tests.rs index 3fc6d4d..f012a22 100644 --- a/app/src-tauri/src/aws/pricing/tests.rs +++ b/app/src-tauri/src/aws/pricing/tests.rs @@ -371,3 +371,27 @@ async fn a_hanging_api_times_out_to_none() { assert_eq!(started.elapsed(), FETCH_TIMEOUT); assert_eq!(api.calls(), 1); } + +#[tokio::test(start_paused = true)] +async fn building_the_client_counts_against_the_same_timeout() { + let (_d, c) = cache(); + c.write("us-west-2", &RATES, at(T0)).unwrap(); + let started = tokio::time::Instant::now(); + // The client never gets built (credentials that hang): stale answers at + // the fetch's own bound, not later. + let r = c + .rates_with::(std::future::pending(), "us-west-2", at(T0 + 2 * DAY)) + .await; + assert_eq!(r, Some(RATES)); + assert_eq!(started.elapsed(), FETCH_TIMEOUT); + + // A fresh cache never builds a client at all. + let fresh = c + .rates_with::( + async { panic!("no client is built for a fresh cache") }, + "us-west-2", + at(T0 + 60), + ) + .await; + assert_eq!(fresh, Some(RATES)); +} diff --git a/app/src-tauri/src/commands/host_commands.rs b/app/src-tauri/src/commands/host_commands.rs index 2d5b485..6daa72e 100644 --- a/app/src-tauri/src/commands/host_commands.rs +++ b/app/src-tauri/src/commands/host_commands.rs @@ -350,14 +350,14 @@ async fn cost_estimate( _ => { // Only now touch the connection (a keys connection reads the // keychain); without one, a stale cache still answers. - let api = match aws_conn(connection_id, settings) { - Ok(conn) => sdk_config(&conn, pricing::PRICING_REGION) - .await - .ok() - .map(|cfg| SdkPricingApi::new(&cfg)), - Err(_) => None, + // Built inside the fetch's 8 s bound: reading a profile's + // credentials can hang as well (an SSO or process provider). + let api = async { + let conn = aws_conn(connection_id, settings).ok()?; + let cfg = sdk_config(&conn, pricing::PRICING_REGION).await.ok()?; + Some(SdkPricingApi::new(&cfg)) }; - cache.rates(api.as_ref(), ®ion, now).await + cache.rates_with::(api, ®ion, now).await } }?; Some(pricing::estimate(config, &rates.for_arch(config.arch)?))