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

handle neg expiry #1418

Merged
merged 3 commits into from
Sep 1, 2021
Merged

handle neg expiry #1418

merged 3 commits into from
Sep 1, 2021

Conversation

jennyf19
Copy link
Collaborator

@jennyf19 jennyf19 commented Aug 31, 2021

@jennyf19 jennyf19 requested a review from jmprieur August 31, 2021 20:10
Copy link
Collaborator

@jmprieur jmprieur left a 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

LoggerMessage.Define<string, string>(
LogLevel.Debug,
LoggingEventId.MemoryCacheNegativeExpiry,
"[MsIdWeb] {CacheType}: {Operation} The SuggestedCacheExpiry from MSAL was negative. ");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe the TokenCacheNotificationArgs.SuggestedCacheExpiry ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed. thanks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@@ -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,
Copy link
Member

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?

Copy link
Collaborator Author

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.

Copy link
Member

@bgavrilMS bgavrilMS Aug 31, 2021

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.

Copy link
Collaborator Author

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)
Copy link
Member

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?

Copy link
Collaborator Author

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);
Copy link
Member

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.

Copy link
Collaborator Author

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.

@sonarqubecloud
Copy link

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

75.0% 75.0% Coverage
0.0% 0.0% Duplication

// Arrange
byte[] cache = new byte[3];
AssertCacheValues(_testCacheAdapter);
Assert.Equal(0, _testCacheAdapter._memoryCache.Count);
Copy link
Member

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

Copy link
Member

@bgavrilMS bgavrilMS left a 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.

@jennyf19 jennyf19 merged commit 6be9af7 into master Sep 1, 2021
@jennyf19 jennyf19 deleted the jennyf/negExpiry branch September 1, 2021 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants