Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

ChangeToken.OnChange fires twice when listening for configuration changes #624

@magnusbakken

Description

@magnusbakken

When I use ChangeToken.OnChange to listen to configuration changes as described in #432, my handler usually gets called twice in quick succession.

To reproduce:

  1. Create a new ASP.NET Core targeting .NET Framework project using the Web API template (I haven't tested targeting .NET Core).
  2. Add this line in the Configure method in Startup.cs: ChangeToken.OnChange(Configuration.GetReloadToken, () => loggerFactory.CreateLogger<Startup>().LogWarning("Configuration changed"));
  3. Start the app.
  4. While the app is running, make a change in appsettings.json and save the file.

The line "Configuration changed" shows up twice in the console output.

I've tried to make the file change from multiple programs (Visual Studio, Notepad, emacs), and by copying and replacing the file in its entirety. At least once I think I saw the change only being logged once, but usually you get it twice with just a few milliseconds between the log events.

I've attached the call stacks I see from the debugger for the first and second call respectively:
first-callstack.txt
second-callstack.txt

The only difference is the following five lines in the middle of the second stack:

mscorlib.dll!System.Threading.CancellationTokenSource.InternalRegister(System.Action<object> callback, object stateForCallback, System.Threading.SynchronizationContext targetSyncContext, System.Threading.ExecutionContext executionContext)	Unknown
mscorlib.dll!System.Threading.CancellationToken.Register(System.Action<object> callback, object state, bool useSynchronizationContext, bool useExecutionContext)	Unknown
mscorlib.dll!System.Threading.CancellationToken.Register(System.Action<object> callback, object state)	Unknown
Microsoft.Extensions.Primitives.dll!Microsoft.Extensions.Primitives.CancellationChangeToken.RegisterChangeCallback(System.Action<object> callback, object state)	Unknown
Microsoft.Extensions.Primitives.dll!Microsoft.Extensions.Primitives.ChangeToken.OnChange.AnonymousMethod__0(object s)	Unknown

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions