Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.
Merged
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
28 changes: 16 additions & 12 deletions src/Microsoft.AspNet.Server.Kestrel/Http/Listener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,22 @@ public Task StartAsync(
Thread = thread;
Application = application;

var tcs = new TaskCompletionSource<int>();
Thread.Post(_ =>
var tcs = new TaskCompletionSource<int>(this);

Thread.Post(tcs2 =>
{
try
{
ListenSocket = CreateListenSocket();
tcs.SetResult(0);
var listener = ((Listener)tcs2.Task.AsyncState);
listener.ListenSocket = listener.CreateListenSocket();
tcs2.SetResult(0);
}
catch (Exception ex)
{
tcs.SetException(ex);
tcs2.SetException(ex);
}
}, null);
}, tcs);

return tcs.Task;
}

Expand Down Expand Up @@ -85,21 +88,22 @@ public void Dispose()
// the exception that stopped the event loop will never be surfaced.
if (Thread.FatalError == null && ListenSocket != null)
{
var tcs = new TaskCompletionSource<int>();
var tcs = new TaskCompletionSource<int>(this);
Thread.Post(
_ =>
tcs2 =>
{
try
{
ListenSocket.Dispose();
tcs.SetResult(0);
var socket = (Listener)tcs2.Task.AsyncState;
socket.ListenSocket.Dispose();
tcs2.SetResult(0);
}
catch (Exception ex)
{
tcs.SetException(ex);
tcs2.SetException(ex);
}
},
null);
tcs);

// REVIEW: Should we add a timeout here to be safe?
tcs.Task.Wait();
Expand Down