-
-
Notifications
You must be signed in to change notification settings - Fork 328
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
Token reloading with RwLock #835
Conversation
Codecov Report
@@ Coverage Diff @@
## master #835 +/- ##
==========================================
- Coverage 69.97% 69.95% -0.02%
==========================================
Files 59 59
Lines 4140 4141 +1
==========================================
Hits 2897 2897
- Misses 1243 1244 +1
Continue to review full report at Codecov.
|
kube-client/src/client/auth/mod.rs
Outdated
let mut locked = token_file.lock().await; | ||
if let Some(token) = locked.cached_token() { | ||
bearer_header(token) | ||
if let Some(header) = { let guard = token_file.read().await; guard.cached_token().map(bearer_header) } { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hah, aggressive scoping. i think this makes sense, but will double check with controller-rs just in case.
it might make sense to put a comment on this line to explain the scope, or maybe see if we can put a unit test around to_header
in the ::File case
This does have the downside (compared to the Mutex) that it can end up reloading multiple times every time the TTL expires. I also wonder if we'd rather explicitly |
If locks happen like the following:
The second
Yeah, that's probably better. |
Ideally, yeah. Then again, I'm not sure it's a huge problem since it should already be in the OS' page cache at that point... |
Signed-off-by: kazk <kazk.dev@gmail.com>
Signed-off-by: kazk <kazk.dev@gmail.com>
Signed-off-by: kazk <kazk.dev@gmail.com>
Signed-off-by: kazk <kazk.dev@gmail.com>
Do you mean it's still possible? fn token(&mut self) -> &str {
if self.is_expiring() { // must be false in the second `W`, right?
if let Ok(token) = std::fs::read_to_string(&self.path) {
self.token = SecretString::from(token);
}
self.expires_at = Utc::now() + Duration::seconds(60);
}
self.token.expose_secret()
} |
Ah okay, that makes sense then. I /think/ it might be slightly clearer to have both checks in the same function (nothing else should use |
Signed-off-by: kazk <kazk.dev@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM then. We might want to consider normalizing this logic across all three token sources at some point, but that feels like a future problem to tackle.
Yeah, the more immediate thing here is probably some kind of e2e test for the token loading (#832) along with a fix for the import issue (#833) so that users don't keep experiencing this issue. Already saw someone run into it again in on kube-rs/controller-rs#26 and it's not a great look. For future bugs like this I think we should probably yank the release since it doesn't leave the crate working very well. This fix looks great to me otoh! |
FWIW, yanking wouldn't have fixed that issue, since it was already locked to the broken version. |
oh. i thought #833 implied that users who manually bumped the |
#833 is more about the opposite issue, people who explicitly depend on
I'm not sure. Looks like cargo-audit does, which implies to me that |
Signed-off-by: kazk <kazk.dev@gmail.com>
The original implementation deadlocked (#829) because it was accidentally trying to lock for write while holding read lock, then attempting to read.
The guard not dropped before
else
was surprising, so here's a minimal demo showing when a temporary value is dropped: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=401eaf29aecf31d4e2d434d005e1c24bI've tested this fix with in-cluster
controller-rs
and it's been working fine, but please test it again. I'm assumingRwLock
is more efficient thanMutex
for this, but I haven't benchmarked. If we want to keepMutex
, we should removecached_token
.