Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Fixed shutdown hanging #47

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions src/Microsoft.AspNet.Server.Kestrel/Http/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public Connection(ListenerContext context, UvStreamHandle socket) : base(context
{
_socket = socket;
ConnectionControl = this;

Thread.OnStopping += OnLoopStopping;
}

public void Start()
Expand All @@ -73,6 +75,11 @@ public void Start()
_socket.ReadStart(_allocCallback, _readCallback, this);
}

private void OnLoopStopping()
{
_socket.Dispose();
}

private Libuv.uv_buf_t OnAlloc(UvStreamHandle handle, int suggestedSize)
{
return handle.Libuv.buf_init(
Expand Down
30 changes: 16 additions & 14 deletions src/Microsoft.AspNet.Server.Kestrel/Infrastructure/KestrelThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class KestrelThread
Queue<CloseHandle> _closeHandleRunning = new Queue<CloseHandle>();
object _workSync = new Object();
bool _stopImmediate = false;
volatile bool _aborting = false;
private ExceptionDispatchInfo _closeError;

public KestrelThread(KestrelEngine engine)
Expand All @@ -41,6 +42,8 @@ public KestrelThread(KestrelEngine engine)

public Action<Action<IntPtr>, IntPtr> QueueCloseHandle { get; internal set; }

public event Action OnStopping;

public Task StartAsync()
{
var tcs = new TaskCompletionSource<int>();
Expand All @@ -61,6 +64,7 @@ public void Stop(TimeSpan timeout)
#endif
}
}

if (_closeError != null)
{
_closeError.Throw();
Expand All @@ -69,7 +73,13 @@ public void Stop(TimeSpan timeout)

private void OnStop(object obj)
{
_post.Unreference();
if (OnStopping != null)
{
OnStopping();
}

_aborting = true;
_post.Dispose();
}

private void OnStopImmediate(object obj)
Expand All @@ -80,6 +90,11 @@ private void OnStopImmediate(object obj)

public void Post(Action<object> callback, object state)
{
if (_aborting)
{
return;
}

lock (_workSync)
{
_workAdding.Enqueue(new Work { Callback = callback, State = state });
Expand Down Expand Up @@ -130,19 +145,6 @@ private void ThreadStart(object parameter)
return;
}

// run the loop one more time to delete the open handles
_post.Reference();
_post.DangerousClose();
_engine.Libuv.walk(
_loop,
(ptr, arg) =>
{
var handle = UvMemory.FromIntPtr<UvHandle>(ptr);
handle.Dispose();
},
IntPtr.Zero);
var ran2 = _loop.Run();

_loop.Dispose();
}
catch (Exception ex)
Expand Down