KestrelThread.Stop should wait for StartAsync to complete

- Previously KestrelThread.Stop might assume start failed prematurely
- This could cause a background thread to run indefinitely in the background
This commit is contained in:
Stephen Halter 2016-02-29 10:35:43 -08:00
parent 9c64a49261
commit 4e5920fd09
1 changed files with 21 additions and 15 deletions

View File

@ -35,7 +35,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel
private Queue<Work> _workRunning = new Queue<Work>(1024); private Queue<Work> _workRunning = new Queue<Work>(1024);
private Queue<CloseHandle> _closeHandleAdding = new Queue<CloseHandle>(256); private Queue<CloseHandle> _closeHandleAdding = new Queue<CloseHandle>(256);
private Queue<CloseHandle> _closeHandleRunning = new Queue<CloseHandle>(256); private Queue<CloseHandle> _closeHandleRunning = new Queue<CloseHandle>(256);
private readonly object _workSync = new Object(); private readonly object _workSync = new object();
private readonly object _startSync = new object();
private bool _stopImmediate = false; private bool _stopImmediate = false;
private bool _initCompleted = false; private bool _initCompleted = false;
private ExceptionDispatchInfo _closeError; private ExceptionDispatchInfo _closeError;
@ -84,9 +85,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel
public void Stop(TimeSpan timeout) public void Stop(TimeSpan timeout)
{ {
if (!_initCompleted) lock (_startSync)
{ {
return; if (!_initCompleted)
{
return;
}
} }
if (_thread.IsAlive) if (_thread.IsAlive)
@ -209,20 +213,22 @@ namespace Microsoft.AspNetCore.Server.Kestrel
private void ThreadStart(object parameter) private void ThreadStart(object parameter)
{ {
var tcs = (TaskCompletionSource<int>)parameter; lock (_startSync)
try
{ {
_loop.Init(_engine.Libuv); var tcs = (TaskCompletionSource<int>)parameter;
_post.Init(_loop, OnPost, EnqueueCloseHandle); try
tcs.SetResult(0); {
_loop.Init(_engine.Libuv);
_post.Init(_loop, OnPost, EnqueueCloseHandle);
_initCompleted = true;
tcs.SetResult(0);
}
catch (Exception ex)
{
tcs.SetException(ex);
return;
}
} }
catch (Exception ex)
{
tcs.SetException(ex);
return;
}
_initCompleted = true;
try try
{ {