From ee3681c20d201245a3dc842f1de48f4efd33d768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teo=20Klestrup=20R=C3=B6ijezon?= Date: Wed, 16 Feb 2022 18:41:18 +0100 Subject: [PATCH] Merge pull request #830 from kube-rs/test-hangs2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix deadlock in token reloading Signed-off-by: Teo Klestrup Röijezon --- kube-client/src/client/auth/mod.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/kube-client/src/client/auth/mod.rs b/kube-client/src/client/auth/mod.rs index 56bb731e5..ba8ed59d7 100644 --- a/kube-client/src/client/auth/mod.rs +++ b/kube-client/src/client/auth/mod.rs @@ -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}; @@ -148,7 +148,7 @@ impl TokenFile { #[derive(Debug, Clone)] pub enum RefreshableToken { Exec(Arc, AuthInfo)>>), - File(Arc>), + File(Arc>), #[cfg(feature = "oauth")] GcpOauth(Arc>), } @@ -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()) } } @@ -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)?), )))); }