Skip to content

Conversation

@Tratcher
Copy link
Member

Fixes #90779

AddMetrics uses an options validation workaround to initialize the MetricsSubscriptionManager and IMetricsListeners. However, if AddMetrics is called multiple times, it will do the initialization multiple times. IMetricsListener implementations may not be expecting that.

This adjusts the options validation logic to make sure initialize is only called once.

Suggest backporting to release/8.0 (but not RC1).

@Tratcher Tratcher requested a review from JamesNK August 21, 2023 22:45
@Tratcher Tratcher self-assigned this Aug 21, 2023
@JamesNK
Copy link
Member

JamesNK commented Aug 22, 2023

Does services.AddOptions<NoOpOptions>().ValidateOnStart(); register anything multiple times?

@Tratcher
Copy link
Member Author

Does services.AddOptions<NoOpOptions>().ValidateOnStart(); register anything multiple times?

This bit might, but since it's a set operation it doesn't cause duplicate work later.

optionsBuilder.Services.AddTransient<IStartupValidator, StartupValidator>();
optionsBuilder.Services.AddOptions<StartupValidatorOptions>()
.Configure<IOptionsMonitor<TOptions>>((vo, options) =>
{
// This adds an action that resolves the options value to force evaluation
// We don't care about the result as duplicates are not important
vo._validators[(typeof(TOptions), optionsBuilder.Name)] = () => options.Get(optionsBuilder.Name);
});

The types aren't public so I can't do it myself.

@Tratcher Tratcher merged commit 85a9b1b into dotnet:main Aug 22, 2023
@Tratcher Tratcher deleted the tratcher/doubleoptions branch August 22, 2023 16:34
@Tratcher
Copy link
Member Author

/backport to release/8.0

@github-actions
Copy link
Contributor

Started backporting to release/8.0: https://github.com/dotnet/runtime/actions/runs/5941582906

@ghost ghost locked as resolved and limited conversation to collaborators Sep 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AddMetrics can initialize MetricsSubscriptionManager multiple times

2 participants