Skip to content

Fix hosting exception during startup when CTRL+C #8886

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/Aspire.Hosting/Dcp/DcpExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public async Task RunApplicationAsync(CancellationToken cancellationToken = defa

await CreateContainersAndExecutablesAsync(cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
// This is here so hosting does not throw an exception when CTRL+C during startup.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would also be good to log a message here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume LogDebug should be used based on other log calls in the same file, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please

_logger.LogDebug("Cancellation received during application startup.");
}
catch
{
_shutdownCancellation.Cancel();
Expand Down
31 changes: 31 additions & 0 deletions tests/Aspire.Hosting.Tests/Dcp/DcpExecutorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,37 @@ public async Task ContainersArePassedExpectedImagePullPolicy()
Assert.Equal(ContainerPullPolicy.Missing, explicitMissingContainer.Spec.PullPolicy);
}

[Fact]
public async Task CancelTokenDuringStartup()
{
// Arrange
var builder = DistributedApplication.CreateBuilder();

const int desiredTargetPort = TestKubernetesService.StartOfAutoPortRange - 999;
builder.AddContainer("database", "image")
.WithEndpoint(name: "NoPortTargetPortSet", targetPort: desiredTargetPort, env: "NO_PORT_TARGET_PORT_SET", isProxied: true);

var kubernetesService = new TestKubernetesService();

using var app = builder.Build();
var distributedAppModel = app.Services.GetRequiredService<DistributedApplicationModel>();
var dcpEvents = new DcpExecutorEvents();
var tokenSource = new CancellationTokenSource();
dcpEvents.Subscribe<OnResourcesPreparedContext>((context) =>
{
tokenSource.Cancel();
return Task.CompletedTask;
});

var appExecutor = CreateAppExecutor(distributedAppModel, kubernetesService: kubernetesService, events: dcpEvents);

// Act
await appExecutor.RunApplicationAsync(tokenSource.Token);

// Assert
Assert.True(tokenSource.IsCancellationRequested);
}

private static void HasKnownCommandAnnotations(IResource resource)
{
var commandAnnotations = resource.Annotations.OfType<ResourceCommandAnnotation>().ToList();
Expand Down