Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.69.x] Fix deadlock in token reloading #831

Merged
merged 1 commit into from
Feb 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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