Clean up if an exception is thrown in the middle of ServerFactory.Start

This commit is contained in:
Stephen Halter 2015-08-25 12:41:11 -07:00
parent c50aec1729
commit 1584d70e1f
1 changed files with 32 additions and 18 deletions

View File

@ -36,30 +36,44 @@ namespace Microsoft.AspNet.Server.Kestrel
public IDisposable Start(IFeatureCollection serverFeatures, Func<IFeatureCollection, Task> application) public IDisposable Start(IFeatureCollection serverFeatures, Func<IFeatureCollection, Task> application)
{ {
var disposables = new List<IDisposable>(); var disposables = new Stack<IDisposable>();
var information = (KestrelServerInformation)serverFeatures.Get<IKestrelServerInformation>(); var disposer = new Disposable(() =>
var engine = new KestrelEngine(_libraryManager, _appShutdownService);
engine.Start(information.ThreadCount == 0 ? 1 : information.ThreadCount);
foreach (var address in information.Addresses)
{
disposables.Add(engine.CreateServer(
address.Scheme,
address.Host,
address.Port,
async frame =>
{
var request = new ServerRequest(frame);
await application.Invoke(request.Features).ConfigureAwait(false);
}));
}
disposables.Add(engine);
return new Disposable(() =>
{ {
foreach (var disposable in disposables) foreach (var disposable in disposables)
{ {
disposable.Dispose(); disposable.Dispose();
} }
}); });
try
{
var information = (KestrelServerInformation)serverFeatures.Get<IKestrelServerInformation>();
var engine = new KestrelEngine(_libraryManager, _appShutdownService);
disposables.Push(engine);
engine.Start(information.ThreadCount == 0 ? 1 : information.ThreadCount);
foreach (var address in information.Addresses)
{
disposables.Push(engine.CreateServer(
address.Scheme,
address.Host,
address.Port,
async frame =>
{
var request = new ServerRequest(frame);
await application.Invoke(request.Features).ConfigureAwait(false);
}));
}
return disposer;
}
catch
{
disposer.Dispose();
throw;
}
} }
} }
} }