Skip to content

Commit

Permalink
Merge pull request #830 from kube-rs/test-hangs2
Browse files Browse the repository at this point in the history
Fix deadlock in token reloading

Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>
  • Loading branch information
nightkr committed Feb 16, 2022
1 parent 64e9ba3 commit ee3681c
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions kube-client/src/client/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use jsonpath_lib::select as jsonpath_select;
use secrecy::{ExposeSecret, SecretString};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::sync::{Mutex, RwLock};
use tokio::sync::Mutex;
use tower::{filter::AsyncPredicate, BoxError};

use crate::config::{AuthInfo, AuthProviderConfig, ExecConfig};
Expand Down Expand Up @@ -148,7 +148,7 @@ impl TokenFile {
#[derive(Debug, Clone)]
pub enum RefreshableToken {
Exec(Arc<Mutex<(SecretString, DateTime<Utc>, AuthInfo)>>),
File(Arc<RwLock<TokenFile>>),
File(Arc<Mutex<TokenFile>>),
#[cfg(feature = "oauth")]
GcpOauth(Arc<Mutex<oauth::Gcp>>),
}
Expand Down Expand Up @@ -206,10 +206,11 @@ impl RefreshableToken {
}

RefreshableToken::File(token_file) => {
if let Some(token) = token_file.read().await.cached_token() {
let mut locked = token_file.lock().await;
if let Some(token) = locked.cached_token() {
bearer_header(token)
} else {
bearer_header(token_file.write().await.token())
bearer_header(locked.token())
}
}

Expand Down Expand Up @@ -278,7 +279,7 @@ impl TryFrom<&AuthInfo> for Auth {
// Token file reference. Must be reloaded at least once a minute.
if let Some(file) = &auth_info.token_file {
return Ok(Self::RefreshableToken(RefreshableToken::File(Arc::new(
RwLock::new(TokenFile::new(file)?),
Mutex::new(TokenFile::new(file)?),
))));
}

Expand Down

0 comments on commit ee3681c

Please sign in to comment.