Skip to content

Commit 14a6f4c

Browse files
Copilotrosebyte
andauthored
Fix Hosting shutdown output subscription race
Co-authored-by: rosebyte <14963300+rosebyte@users.noreply.github.com>
1 parent bd3dc0c commit 14a6f4c

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

src/libraries/Microsoft.Extensions.Hosting/tests/FunctionalTests/IntegrationTesting/src/Deployers/SelfHostDeployer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class SelfHostDeployer : ApplicationDeployer
2020
private const string ApplicationStartedMessage = "Application started. Press Ctrl+C to shut down.";
2121

2222
public Process HostProcess { get; private set; }
23+
public event DataReceivedEventHandler OutputReceived;
2324

2425
public SelfHostDeployer(DeploymentParameters deploymentParameters, ILoggerFactory loggerFactory)
2526
: base(deploymentParameters, loggerFactory)
@@ -130,6 +131,8 @@ protected async Task<CancellationToken> StartSelfHostAsync()
130131
{
131132
started.TrySetResult(null);
132133
}
134+
135+
OutputReceived?.Invoke(sender, dataArgs);
133136
};
134137
var hostExitTokenSource = new CancellationTokenSource();
135138
HostProcess.Exited += (sender, e) =>

src/libraries/Microsoft.Extensions.Hosting/tests/FunctionalTests/ShutdownTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,10 @@ private async Task ExecuteShutdownTest(string testName, string shutdownMechanic)
7171

7272
using (var deployer = new SelfHostDeployer(deploymentParameters, xunitTestLoggerFactory))
7373
{
74-
var result = await deployer.DeployAsync();
75-
7674
var started = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
7775
var completed = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
7876
var output = string.Empty;
79-
deployer.HostProcess.OutputDataReceived += (sender, args) =>
77+
deployer.OutputReceived += (sender, args) =>
8078
{
8179
if (!string.IsNullOrEmpty(args.Data) && args.Data.StartsWith(StartedMessage))
8280
{
@@ -94,6 +92,8 @@ private async Task ExecuteShutdownTest(string testName, string shutdownMechanic)
9492
}
9593
};
9694

95+
await deployer.DeployAsync();
96+
9797
await started.Task.WaitAsync(TimeSpan.FromSeconds(180));
9898

9999
SendShutdownSignal(deployer.HostProcess);

0 commit comments

Comments
 (0)