提交 ade30af4 编写于 作者: S Sam Harwell

Fix incorrect assumption about EndListening and completed tasks

上级 4b1630c0
......@@ -61,7 +61,8 @@ internal interface IClientConnectionHost
/// <summary>
/// Stop accepting new connections. It will also ensure that the last return from
/// <see cref="GetNextClientConnectionAsync"/> is in a completed state.
/// <see cref="GetNextClientConnectionAsync"/> is either already in a completed state, or has scheduled an
/// operation which will transition the task to a completed state.
/// </summary>
void EndListening();
}
......
......@@ -102,20 +102,28 @@ public void ListenAndDispatchConnections(TimeSpan? keepAlive, CancellationToken
_clientConnectionHost.EndListening();
}
// This type is responsible for cleaning up resources associated with _listenTask. Once EndListening
// is complete this task is guaranteed to be completed. If it ran to completion we need to
// dispose of the value.
Console.WriteLine(_listenTask?.Status);
Debug.Assert(_listenTask is null || _listenTask.IsCompleted);
if (_listenTask?.Status == TaskStatus.RanToCompletion)
if (_listenTask is not null)
{
try
// This type is responsible for cleaning up resources associated with _listenTask. Once EndListening
// is complete this task is guaranteed to be either completed or have a task scheduled to complete
// it. If it ran to completion we need to dispose of the value.
if (!_listenTask.IsCompleted)
{
_listenTask.Result.Dispose();
// Wait for the task to complete
_listenTask.ContinueWith(_ => { }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default)
.Wait(CancellationToken.None);
}
catch (Exception ex)
if (_listenTask.Status == TaskStatus.RanToCompletion)
{
CompilerServerLogger.LogException(ex, $"Error disposing of {nameof(_listenTask)}");
try
{
_listenTask.Result.Dispose();
}
catch (Exception ex)
{
CompilerServerLogger.LogException(ex, $"Error disposing of {nameof(_listenTask)}");
}
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册