From 009eec764e72b28d9fd9592f6ca27ee7d266d63f Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Tue, 24 Nov 2026 12:16:13 -0500 Subject: [PATCH] wait for unlock before responding to credentials request --- src/session.rs | 39 ++++++++++++++++++++++++++++++++++++++- src/srv/creddy_server.rs | 13 +++++++++---- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/session.rs b/src/session.rs index c8387d3..8853e92 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,6 +1,6 @@ use crate::config::AppConfig; use crate::db::Db; -use gpui_kit::Global; +use gpui_kit::{AsyncApp, Global}; #[derive(Debug)] pub enum AppSession { @@ -17,6 +17,22 @@ impl AppSession { AppSession::Empty } } + + /// Resolves once the session is unlocked, which is immediately if it already is. + pub async fn wait_for_unlock(cx: &AsyncApp) { + let (tx, rx) = smol::channel::bounded(1); + let _subscription = cx.update(|cx| { + cx.observe_global::(move |_| { + // a full channel already means "check again", so a failed send loses nothing + let _ = tx.try_send(()); + }) + }); + + while !cx.read_global::(|session, _| matches!(session, AppSession::Unlocked(_))) { + // the subscription owns the sender, and outlives this loop + rx.recv().await.expect("session observer was dropped"); + } + } } impl Global for AppSession {} @@ -25,6 +41,7 @@ impl Global for AppSession {} mod tests { use super::*; use crate::tests::TestEnv; + use gpui_kit::TestAppContext; use std::assert_matches; use testresult::TestResult; @@ -44,4 +61,24 @@ mod tests { assert_matches!(session, AppSession::Locked); Ok(()) } + + #[gpui_kit::test] + fn test_wait_for_unlock(cx: &mut TestAppContext) { + let env = TestEnv::new().unwrap(); + let db = env.db().unwrap(); + cx.update(|cx| cx.set_global(AppSession::Locked)); + + let task = cx.spawn(async move |cx| AppSession::wait_for_unlock(&cx).await); + cx.run_until_parked(); + assert!(!task.is_ready()); + + // a change that doesn't unlock shouldn't end the wait + cx.update(|cx| cx.set_global(AppSession::Locked)); + cx.run_until_parked(); + assert!(!task.is_ready()); + + cx.update(|cx| cx.set_global(AppSession::Unlocked(db))); + cx.run_until_parked(); + assert!(task.is_ready()); + } } diff --git a/src/srv/creddy_server.rs b/src/srv/creddy_server.rs index 8c7c245..6d51bc4 100644 --- a/src/srv/creddy_server.rs +++ b/src/srv/creddy_server.rs @@ -148,19 +148,24 @@ async fn handle_aws_get(uri: Uri, app: AsyncApp) -> Result> let params: HashMap<_, _> = form_urlencoded::parse(qs).collect(); let name = params.get("name").map(|n| n.to_string()); let session = params.get("session").map(|s| s == "true").unwrap_or(false); + + // we need to hang on to `name` so we can use it to retrieve the credential later let req = CredentialRequest::AwsGet { name: name.clone(), session }; let (sender, receiver) = oneshot::channel::(); - app.update_global::(|pending, _cx| pending.0.push_back((req.clone(), sender))); + app.update_global::(|pending, _cx| pending.0.push_back((req, sender))); let resp = receiver.await.unwrap_or_else(|_| CredentialResponse::no()); if !resp.approved { return Err(CreddyError::RequestDenied); } - // eventually: let session = AppSession::wait_for_unlock().await; or something like that - let creds = app.read_global::(|session, cx| { - let AppSession::Unlocked(db) = session else { unreachable!() }; + // it's possible to approve a request while the app is still locked, so we have to account for that + AppSession::wait_for_unlock(&app).await; + let creds = app.read_global::(|session, _cx| { + let AppSession::Unlocked(db) = session else { + unreachable!("App re-locked immediately after unlock") + }; db.load_aws_credential(&name.unwrap_or_else(|| "default".into())) })?;