runtime icon indicating copy to clipboard operation
runtime copied to clipboard

OptionsCache fails to recover from initialization failure

Open RaffaelHold opened this issue 6 years ago • 7 comments

Description

When passing a factory method - that might throw a runtime exception - as a parameter to .GetOrAdd() on an instance of OptionsCache the cache will never recover if an exception gets thrown in the factory method.

This happens because the underlying Lazy will never try to reinitialize its value even if the error condition goes away.

For example Microsoft.Identity.Web does make an remote HTTP call during initialization of JwtBearerOptions. If theres a connection outage, when the factory method is executed, every subsequent cache access will throw an exception.
The only fix is to manually clear the cache or restart the application.

Sample:

using System;
using Microsoft.Extensions.Options; // Microsoft.Extensions.Options @ 3.1.7

namespace ConsoleApp
{
    public static class Program
    {
        static void Main()
        {
            var optionsCache = new OptionsCache<Options>();
            try
            {
                var expectedToFail = optionsCache.GetOrAdd("options", () => new Options());
            }
            catch { }

            var shouldBeSucceeding = optionsCache.GetOrAdd("options", () => new Options());
        }

        private class Options
        {
            private static bool _throws = true;
            public Options()
            {
                if (_throws)
                {
                    _throws = false;
                    throw new Exception();
                }
            }
        }
    }
}

Possible Solutions

a) change the Lazy's LazyThreadSafetyMode to PublicationOnly to allow multiple evaluations until one succeeds (this might have unintended performance consequences)
b) reset the cache entry when an exception is thrown during initialization of the Lazy

Configuration

  • Which version of .NET is the code running on?
    3.1.7
  • What OS and version, and what distro if applicable?
    Windows 10 2004 Build 19041.450
  • What is the architecture (x64, x86, ARM, ARM64)?
    x64
  • Do you know whether it is specific to that configuration?
    It is not specific to configuration

RaffaelHold avatar Sep 04 '20 12:09 RaffaelHold

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

Dotnet-GitSync-Bot avatar Sep 04 '20 12:09 Dotnet-GitSync-Bot

Tagging subscribers to this area: @maryamariyan See info in area-owners.md if you want to be subscribed.

ghost avatar Sep 04 '20 15:09 ghost

@RaffaelHold are you sending the HTTP request during the construction of your Options instance?

ViktorHofer avatar Sep 30 '20 20:09 ViktorHofer

@ViktorHofer I found this issue in Microsoft.Identity.Web where indeed an HTTP request is sent during construction of the Options instance. But any runtime exception will lead to this behavior

Here are the relevant lines in Microsoft.Identity.Web Microsoft.Identity.Web/WebApiExtensions/WebApiAuthenticationBuilderExtensions.cs which leads to a HTTP call being made in AadIssuerValidator.GetIssuerValidator

I hope this gives you a better idea what's happening.

I'd also be happy to give the implementation of a fix a shot if I get some pointers on how we want to resolve this issue.

RaffaelHold avatar Oct 02 '20 08:10 RaffaelHold

Don't make http requests that block during construction of objects. Do it before or after. This could even result in deadlocks if not careful.

Even if the lazy doesn't re-evaluate, this looks like a bad idea and it's weird that options would be re-initialized and retired. Constructions of objects isn't something that should be unreliable and retried (or bake the retries into the code itself)

davidfowl avatar Oct 03 '20 07:10 davidfowl

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

dotnet-policy-service avatar Apr 04 '25 10:04 dotnet-policy-service

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

dotnet-policy-service avatar Apr 18 '25 12:04 dotnet-policy-service