Handle null reference exception when TicketCacheBase.Refresh is null#372
Conversation
| await (this.Refresh?.Invoke(cacheEntry)).ConfigureAwait(false); | ||
| if (this.Refresh != null) | ||
| { | ||
| await this.Refresh.Invoke(cacheEntry).ConfigureAwait(false); |
There was a problem hiding this comment.
This actually regresses to a slightly more problematic issue because Refresh can be nulled out between the null check and the call across threads, whereas this.Refresh?.Invoke ends up being an atomic operation. As such, I think the fix is actually simpler: make it await (this.Refresh?.Invoke(cacheEntry))?.ConfigureAwait(false);
There was a problem hiding this comment.
Build is complaining:
Kerberos.NET\Cache\TicketCacheBase.cs(161,21): Error CS1061: 'ConfiguredTaskAwaitable?' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'ConfiguredTaskAwaitable?' could be found (are you missing a using directive or an assembly reference?)
There was a problem hiding this comment.
Hah, I suppose that's what I get for editing from my phone! I should be able to take a closer look on Friday
|
The CI failure does not appear to be related to the change as build and tests all passed |
|
That's the build service being dumb. I need to fix the build step to not try and push PR nuget packages to the private repo internally. Otherwise change looks good. |
What's the problem?
A
NullReferenceExceptioncan happen when usingKrb5TicketCache.What's the solution?
Evaluate whether
Refreshis null before trying to Invoke it and.ConfigureAwait(false).Background information
I inherited some code that hits a
NullReferenceExceptionwhen the cache tries to refresh. I struggled to write a test for this scenario due to the way the different objects (and default values) are instantiated, the methods being private and the nature of the background tasks involved.I'm still getting up to speed on everything involved, but I believe the most relevant bits I'm starting from include something like this: