-
Notifications
You must be signed in to change notification settings - Fork 219
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
handle neg expiry #1418
handle neg expiry #1418
Conversation
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
Left 2 suggestions
Thanks @jennyf19
src/Microsoft.Identity.Web/TokenCacheProviders/InMemory/MsalMemoryTokenCacheProvider.cs
Show resolved
Hide resolved
LoggerMessage.Define<string, string>( | ||
LogLevel.Debug, | ||
LoggingEventId.MemoryCacheNegativeExpiry, | ||
"[MsIdWeb] {CacheType}: {Operation} The SuggestedCacheExpiry from MSAL was negative. "); |
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.
maybe the TokenCacheNotificationArgs.SuggestedCacheExpiry
?
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.
fixed. thanks
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.
@@ -58,6 +58,12 @@ private static class Logger | |||
LoggingEventId.MemoryCacheRead, | |||
"[MsIdWeb] {CacheType}: {Operation} cacheKey {CacheKey} cache size {Size} "); | |||
|
|||
private static readonly Action<ILogger, string, string, Exception?> s_l1CacheNegativeExpiry = | |||
LoggerMessage.Define<string, string>( | |||
LogLevel.Debug, |
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.
More like a LogLevel.Warning or Error?
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.
i don't think this error is actionable by the service.
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.
They should log a bug on MSAL. This field is only calculated on SaveMsalTokenResponse
, so we assume there is a new access token incomming and no RTs. New ATs should always have a bit of time left on them.
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.
i did.
@@ -237,6 +237,11 @@ protected override async Task WriteCacheBytesAsync(string cacheKey, byte[] bytes | |||
if (cacheSerializerHints != null && cacheSerializerHints?.SuggestedCacheExpiry != null) | |||
{ | |||
cacheExpiry = cacheSerializerHints.SuggestedCacheExpiry.Value.UtcDateTime - DateTime.UtcNow; | |||
if (cacheExpiry < TimeSpan.Zero) |
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.
Hmm, should you not delete the cache entry instead?
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.
we could but then the L1/L2 will be out of sync.
// Assert | ||
Assert.Equal(1, _testCacheAdapter._memoryCache.Count); | ||
Assert.Single(L2Cache.dict); | ||
await Task.Delay(1000).ConfigureAwait(false); |
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.
Generally we should avoid awaiting in tests. But I think in this case the adapter should just delete the entry itself or not set an expiry.
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.
what happens if MSAL expects that the entry was written and doesn't find it just after? I tried to have a void implementation of a serializer and that didn't work. MSAL crashed, it was expecting something to come back.
SonarCloud Quality Gate failed. |
// Arrange | ||
byte[] cache = new byte[3]; | ||
AssertCacheValues(_testCacheAdapter); | ||
Assert.Equal(0, _testCacheAdapter._memoryCache.Count); |
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.
nit: internal member naming is MemoryCache, not _memoryCache
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, I think we should still log an error or a warning, to try to expose this problem somehow and get MSAL fixed.
#1419