Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(desktop): guard stale login-gated service activation after sign-out

Steven Enamakel676cbc87adf2
diff --git a/src/openhuman/desktop/app_state/ops_part_02.rs b/src/openhuman/desktop/app_state/ops_part_02.rs
index 9f3821716a..1f1b2bf9ec 100644
--- a/src/openhuman/desktop/app_state/ops_part_02.rs
+++ b/src/openhuman/desktop/app_state/ops_part_02.rs
@@ -3,6 +3,7 @@ async fn finish_revalidated_user_activation(
target_config: &Config,
user_id: &str,
service_rebind_source: Option<&Config>,
+ generation: u64,
) {
if let Err(error) = crate::openhuman::cron::seed::prune_retired_jobs(target_config) {
warn!("{LOG_PREFIX} failed to prune retired cron jobs after pending session revalidation: {error}");
@@ -30,7 +31,19 @@ async fn finish_revalidated_user_activation(
target_config.workspace_dir.clone(),
);
if let Some(source_config) = service_rebind_source {
+ if current_user_generation() != generation {
+ debug!(
+ "{LOG_PREFIX} skipping stale login-gated service activation after pending session revalidation"
+ );
+ return;
+ }
crate::openhuman::security::credentials::stop_login_gated_services(source_config).await;
+ if current_user_generation() != generation {
+ debug!(
+ "{LOG_PREFIX} skipping stale login-gated service restart after pending session revalidation"
+ );
+ return;
+ }
crate::openhuman::security::credentials::start_login_gated_services(target_config).await;
} else {
debug!(
@@ -64,60 +77,66 @@ async fn persist_revalidated_session_user(
user: Value,
generation: u64,
) -> Result<Box<Config>, String> {
- let _session_mutation_lock = CURRENT_USER_SESSION_MUTATION_LOCK.lock().await;
- if current_user_generation() != generation {
- return Err("pending session persistence became stale after sign-out".to_string());
- }
- let user_id = user_id_from_profile_payload(&user)
- .ok_or_else(|| "backend user id required before clearing pending validation".to_string())?;
- let workspace_env_scoped = config_is_workspace_env_scoped(config);
- let target_config = if !workspace_env_scoped {
- activate_revalidated_user_dir(&user_id).await?
- } else {
- debug!(
- "{LOG_PREFIX} keeping revalidated pending session in OPENHUMAN_WORKSPACE-scoped config"
- );
- config.clone()
- };
- let source_config = config.clone();
- let source_moved = !same_config_state_dir(config, &target_config);
- let token = token.to_string();
- let mut metadata: HashMap<String, String> = base_metadata.into_iter().collect();
- metadata.insert("user_id".to_string(), user_id.clone());
- metadata.insert("user_json".to_string(), user.to_string());
-
- let config_for_store = target_config.clone();
- tokio::task::spawn_blocking(move || {
- AuthService::from_config(&config_for_store)
- .store_provider_token(
- APP_SESSION_PROVIDER,
- DEFAULT_AUTH_PROFILE_NAME,
- &token,
- metadata,
- true,
- )
- .map(|_| ())
- .map_err(|e| e.to_string())
- })
- .await
- .unwrap_or_else(|e| {
- Err(format!(
- "{LOG_PREFIX} revalidated session persist task panicked: {e}"
- ))
- })?;
-
- if source_moved {
- if let Err(error) = remove_revalidated_source_profile(&source_config).await {
- warn!(
- "{LOG_PREFIX} failed to remove source pending session profile after user activation: {error}"
+ let (target_config, user_id, source_config, source_moved) = {
+ let _session_mutation_lock = CURRENT_USER_SESSION_MUTATION_LOCK.lock().await;
+ if current_user_generation() != generation {
+ return Err("pending session persistence became stale after sign-out".to_string());
+ }
+ let user_id = user_id_from_profile_payload(&user).ok_or_else(|| {
+ "backend user id required before clearing pending validation".to_string()
+ })?;
+ let workspace_env_scoped = config_is_workspace_env_scoped(config);
+ let target_config = if !workspace_env_scoped {
+ activate_revalidated_user_dir(&user_id).await?
+ } else {
+ debug!(
+ "{LOG_PREFIX} keeping revalidated pending session in OPENHUMAN_WORKSPACE-scoped config"
);
+ config.clone()
+ };
+ let source_config = config.clone();
+ let source_moved = !same_config_state_dir(config, &target_config);
+ let token = token.to_string();
+ let mut metadata: HashMap<String, String> = base_metadata.into_iter().collect();
+ metadata.insert("user_id".to_string(), user_id.clone());
+ metadata.insert("user_json".to_string(), user.to_string());
+
+ let config_for_store = target_config.clone();
+ tokio::task::spawn_blocking(move || {
+ AuthService::from_config(&config_for_store)
+ .store_provider_token(
+ APP_SESSION_PROVIDER,
+ DEFAULT_AUTH_PROFILE_NAME,
+ &token,
+ metadata,
+ true,
+ )
+ .map(|_| ())
+ .map_err(|e| e.to_string())
+ })
+ .await
+ .unwrap_or_else(|e| {
+ Err(format!(
+ "{LOG_PREFIX} revalidated session persist task panicked: {e}"
+ ))
+ })?;
+
+ if source_moved {
+ if let Err(error) = remove_revalidated_source_profile(&source_config).await {
+ warn!(
+ "{LOG_PREFIX} failed to remove source pending session profile after user activation: {error}"
+ );
+ }
}
- }
+
+ (target_config, user_id, source_config, source_moved)
+ };
finish_revalidated_user_activation(
&target_config,
&user_id,
source_moved.then_some(&source_config),
+ generation,
)
.await;
diff --git a/src/openhuman/desktop/app_state/ops_signout_cache_tests.rs b/src/openhuman/desktop/app_state/ops_signout_cache_tests.rs
index ed80ee91db..720629c663 100644
--- a/src/openhuman/desktop/app_state/ops_signout_cache_tests.rs
+++ b/src/openhuman/desktop/app_state/ops_signout_cache_tests.rs
@@ -36,7 +36,7 @@ async fn signing_out_forgets_both_current_user_caches() {
let _env_lock = crate::openhuman::config::TEST_ENV_LOCK
.lock()
.unwrap_or_else(|e| e.into_inner());
- let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock();
+ let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock().await;
let _failure_lock = CURRENT_USER_FAILURE_TEST_LOCK.lock().await;
let _reset = CurrentUserFailureResetGuard;
@@ -101,7 +101,7 @@ async fn pending_revalidation_completing_after_logout_does_not_restore_the_sessi
let _env_lock = crate::openhuman::config::TEST_ENV_LOCK
.lock()
.unwrap_or_else(|e| e.into_inner());
- let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock();
+ let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock().await;
let _failure_lock = CURRENT_USER_FAILURE_TEST_LOCK.lock().await;
let _reset = CurrentUserFailureResetGuard;
@@ -159,7 +159,7 @@ async fn pending_revalidation_completing_after_logout_does_not_restore_the_sessi
#[test]
fn forget_current_user_caches_clears_the_positive_snapshot() {
- let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock();
+ let _cache_lock = APP_STATE_CACHE_TEST_LOCK.blocking_lock();
let _failure_lock = CURRENT_USER_FAILURE_TEST_LOCK.blocking_lock();
let _reset = CurrentUserFailureResetGuard;
@@ -184,7 +184,7 @@ fn forget_current_user_caches_clears_the_positive_snapshot() {
#[test]
fn forget_current_user_caches_clears_the_freshness_stamp() {
- let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock();
+ let _cache_lock = APP_STATE_CACHE_TEST_LOCK.blocking_lock();
let _failure_lock = CURRENT_USER_FAILURE_TEST_LOCK.blocking_lock();
let _reset = CurrentUserStalenessResetGuard;
@@ -208,7 +208,7 @@ fn forget_current_user_caches_clears_the_freshness_stamp() {
#[test]
fn forget_current_user_caches_clears_the_negative_failure() {
- let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock();
+ let _cache_lock = APP_STATE_CACHE_TEST_LOCK.blocking_lock();
let _failure_lock = CURRENT_USER_FAILURE_TEST_LOCK.blocking_lock();
let _reset = CurrentUserFailureResetGuard;
@@ -289,7 +289,7 @@ async fn spawn_racing_backend(
#[tokio::test]
async fn a_successful_refresh_that_races_sign_out_does_not_repopulate_the_snapshot() {
- let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock();
+ let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock().await;
let _failure_lock = CURRENT_USER_FAILURE_TEST_LOCK.lock().await;
let _reset = CurrentUserFailureResetGuard;
@@ -339,7 +339,7 @@ async fn a_successful_refresh_that_races_sign_out_does_not_repopulate_the_snapsh
#[tokio::test]
async fn a_failed_refresh_that_races_sign_out_does_not_record_a_failure() {
- let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock();
+ let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock().await;
let _failure_lock = CURRENT_USER_FAILURE_TEST_LOCK.lock().await;
let _reset = CurrentUserFailureResetGuard;
@@ -382,7 +382,7 @@ async fn a_failed_refresh_that_races_sign_out_does_not_record_a_failure() {
#[tokio::test]
async fn a_sign_out_landing_after_the_generation_check_still_wins_the_snapshot() {
- let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock();
+ let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock().await;
// Both globals are touched here, so both guards are needed. The failure
// lock is async, which is why these three were sync-only and unguarded.
let _failure_lock = CURRENT_USER_FAILURE_TEST_LOCK.lock().await;
@@ -411,7 +411,7 @@ async fn a_sign_out_landing_after_the_generation_check_still_wins_the_snapshot()
#[tokio::test]
async fn a_sign_out_landing_after_the_generation_check_still_wins_the_failure_record() {
- let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock();
+ let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock().await;
// Both globals are touched here, so both guards are needed. The failure
// lock is async, which is why these three were sync-only and unguarded.
let _failure_lock = CURRENT_USER_FAILURE_TEST_LOCK.lock().await;
@@ -437,7 +437,7 @@ async fn a_sign_out_landing_after_the_generation_check_still_wins_the_failure_re
#[tokio::test]
async fn publishing_under_the_current_generation_still_clears_a_recorded_outage() {
- let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock();
+ let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock().await;
// Both globals are touched here, so both guards are needed. The failure
// lock is async, which is why these three were sync-only and unguarded.
let _failure_lock = CURRENT_USER_FAILURE_TEST_LOCK.lock().await;
@@ -485,7 +485,7 @@ async fn publishing_under_the_current_generation_still_clears_a_recorded_outage(
/// the generation handed to the refresh is the one read before it.
#[tokio::test]
async fn a_sign_out_between_the_token_load_and_the_refresh_still_wins() {
- let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock();
+ let _cache_lock = APP_STATE_CACHE_TEST_LOCK.lock().await;
let _failure_lock = CURRENT_USER_FAILURE_TEST_LOCK.lock().await;
let _reset = CurrentUserFailureResetGuard;