Initialize HttpConnectionManager before registering callbacks (#2878)

This commit is contained in:
BrennanConroy 2018-08-30 14:42:16 -07:00 committed by GitHub
parent f91ba38907
commit f42f4c56e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View File

@ -10,7 +10,6 @@ using System.IO;
using System.IO.Pipelines;
using System.Net.WebSockets;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Internal;
@ -36,9 +35,11 @@ namespace Microsoft.AspNetCore.Http.Connections.Internal
{
_logger = loggerFactory.CreateLogger<HttpConnectionManager>();
_connectionLogger = loggerFactory.CreateLogger<HttpConnectionContext>();
_nextHeartbeat = new TimerAwaitable(_heartbeatTickRate, _heartbeatTickRate);
// Register these last as the callbacks could run immediately
appLifetime.ApplicationStarted.Register(() => Start());
appLifetime.ApplicationStopping.Register(() => CloseConnections());
_nextHeartbeat = new TimerAwaitable(_heartbeatTickRate, _heartbeatTickRate);
}
public void Start()

View File

@ -314,6 +314,29 @@ namespace Microsoft.AspNetCore.Http.Connections.Tests
await tcs.Task.OrTimeout();
}
[Fact]
public async Task ApplicationLifetimeCanStartBeforeHttpConnectionManagerInitialized()
{
var appLifetime = new TestApplicationLifetime();
appLifetime.Start();
var connectionManager = CreateConnectionManager(appLifetime);
var tcs = new TaskCompletionSource<object>();
var connection = connectionManager.CreateConnection(PipeOptions.Default, PipeOptions.Default);
connection.Application.Output.OnReaderCompleted((error, state) =>
{
tcs.TrySetResult(null);
},
null);
appLifetime.StopApplication();
// Connection should be disposed so this should complete immediately
await tcs.Task.OrTimeout();
}
private static HttpConnectionManager CreateConnectionManager(IApplicationLifetime lifetime = null)
{
lifetime = lifetime ?? new EmptyApplicationLifetime();