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

Only initialize listeners once #90902

Merged
merged 1 commit into from
Aug 22, 2023
Merged

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).

@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