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

[release/8.0] Ensure exception event source listener is running before applying startup hook #7395

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Tools/dotnet-monitor/Commands/CollectCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,25 @@ private static IHostBuilder Configure(this IHostBuilder builder, StartupAuthenti
services.AddSingleton<ProfilerChannel>();
services.ConfigureCollectionRules();
services.ConfigureLibrarySharing();

//
// The order of the below calls is **important**.
// - ConfigureInProcessFeatures needs to be called before ConfigureProfiler and ConfigureStartupHook
// because these features will configure themselves depending on environment variables set by InProcessFeaturesEndpointInfoSourceCallbacks.
// - ConfigureProfiler needs to be called before ConfigureStartupHook
// because the startup hook may call into the profiler on load.
// - ConfigureExceptions needs to be called before ConfigureStartupHook
// because we want to avoid missing exception data events and potentially having an out-of-sync name cache.
// - ConfigureStartupHook needs to be called before ConfigureHostingStartup
// because the hosting startup assembly depends on the startup hook assembly.
//
services.ConfigureInProcessFeatures(context.Configuration);
services.ConfigureProfiler();
services.ConfigureExceptions();
services.ConfigureStartupHook();
services.ConfigureHostingStartup();
services.ConfigureExceptions();

services.ConfigureStartupLoggers(authConfigurator);
services.ConfigureInProcessFeatures(context.Configuration);
services.AddSingleton<IInProcessFeatures, InProcessFeatures>();
services.AddSingleton<IDumpOperationFactory, DumpOperationFactory>();
services.AddSingleton<ILogsOperationFactory, LogsOperationFactory>();
Expand Down
84 changes: 0 additions & 84 deletions src/Tools/dotnet-monitor/DiagnosticLifetimeBackgroundService.cs

This file was deleted.

35 changes: 24 additions & 11 deletions src/Tools/dotnet-monitor/Exceptions/ExceptionsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.Diagnostics.Monitoring.WebApi;
using Microsoft.Diagnostics.Monitoring.WebApi.Exceptions;
using Microsoft.Diagnostics.NETCore.Client;
using Microsoft.Diagnostics.Tools.Monitor.StartupHook;
using Microsoft.Extensions.Options;
using System;
using System.Threading;
Expand All @@ -17,45 +16,59 @@ namespace Microsoft.Diagnostics.Tools.Monitor.Exceptions
/// Get exception information from target process and store it.
/// </summary>
internal sealed class ExceptionsService :
DiagnosticLifetimeBackgroundService
IDiagnosticLifetimeService, IAsyncDisposable
{
private readonly EventExceptionsPipeline _pipeline;
private readonly IOptions<ExceptionsOptions> _options;
private readonly StartupHookService _startupHookService;
// We don't need to guard against concurrent StartAsync and StopAsync calls
private bool _isStarted;

public ExceptionsService(
IEndpointInfo endpointInfo,
IOptions<ExceptionsOptions> options,
IExceptionsStore store,
StartupHookService startupHookService)
IExceptionsStore store)
{
ArgumentNullException.ThrowIfNull(endpointInfo);
ArgumentNullException.ThrowIfNull(store);

_options = options ?? throw new ArgumentNullException(nameof(options));
_startupHookService = startupHookService ?? throw new ArgumentNullException(nameof(startupHookService));

_pipeline = new EventExceptionsPipeline(
new DiagnosticsClient(endpointInfo.Endpoint),
new EventExceptionsPipelineSettings(),
store);
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
public async ValueTask StartAsync(CancellationToken cancellationToken)
{
if (!_options.Value.GetEnabled() || !await _startupHookService.CheckHasStartupHookAsync(stoppingToken))
if (!_options.Value.GetEnabled())
{
return;
}

// Wrap the passed CancellationToken into a linked CancellationTokenSource so that the
// RunAsync method is only cancellable for the execution of the StartAsync method.
// We don't want the caller to be able to cancel the run of the pipeline after having finished
// executing the StartAsync method.
using CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

// Collect exceptions and place them into exceptions store
await _pipeline.RunAsync(stoppingToken);
await _pipeline.StartAsync(cts.Token);
_isStarted = true;
}

public override async ValueTask DisposeAsync()
public async ValueTask StopAsync(CancellationToken cancellationToken)
{
await base.DisposeAsync();
if (!_isStarted)
{
return;
}

await _pipeline.StopAsync(cancellationToken);
}

public async ValueTask DisposeAsync()
{
await _pipeline.DisposeAsync();
}
}
Expand Down