diff --git a/src/Compilers/Server/VBCSCompiler/NamedPipeClientConnection.cs b/src/Compilers/Server/VBCSCompiler/NamedPipeClientConnection.cs index 0af5fdddec4014c68e5da8a37153b6332edb070e..75aea73c4d1096a2e87a93067f932eda9df0d4ed 100644 --- a/src/Compilers/Server/VBCSCompiler/NamedPipeClientConnection.cs +++ b/src/Compilers/Server/VBCSCompiler/NamedPipeClientConnection.cs @@ -175,30 +175,9 @@ internal NamedPipeClientConnection(ICompilerServerHost compilerServerHost, strin /// /// This will return true if the pipe was disconnected. /// - protected override async Task CreateMonitorDisconnectTask(CancellationToken cancellationToken) + protected override Task CreateMonitorDisconnectTask(CancellationToken cancellationToken) { - var buffer = Array.Empty(); - - while (!cancellationToken.IsCancellationRequested && _pipeStream.IsConnected) - { - // Wait a second before trying again - await Task.Delay(1000, cancellationToken).ConfigureAwait(false); - - try - { - CompilerServerLogger.Log($"Pipe {LoggingIdentifier}: Before poking pipe."); - await _pipeStream.ReadAsync(buffer, 0, 0, cancellationToken).ConfigureAwait(false); - CompilerServerLogger.Log($"Pipe {LoggingIdentifier}: After poking pipe."); - } - catch (Exception e) - { - // It is okay for this call to fail. Errors will be reflected in the - // IsConnected property which will be read on the next iteration of the - // loop - var msg = string.Format($"Pipe {LoggingIdentifier}: Error poking pipe."); - CompilerServerLogger.LogException(e, msg); - } - } + return BuildServerConnection.CreateMonitorDisconnectTask(_pipeStream, LoggingIdentifier, cancellationToken); } protected override void ValidateBuildRequest(BuildRequest request) diff --git a/src/Compilers/Shared/BuildServerConnection.cs b/src/Compilers/Shared/BuildServerConnection.cs index 7d8b9d8d2c9498b395e81dc3829da7e7eea8825d..610674643c166c1fb5d83c4a830c78c1c24ad636 100644 --- a/src/Compilers/Shared/BuildServerConnection.cs +++ b/src/Compilers/Shared/BuildServerConnection.cs @@ -228,7 +228,7 @@ internal static string GetRuntimeDirectoryOpt() Log("Begin reading response"); var responseTask = BuildResponse.ReadAsync(pipeStream, serverCts.Token); - var monitorTask = CreateMonitorDisconnectTask(pipeStream, serverCts.Token); + var monitorTask = CreateMonitorDisconnectTask(pipeStream, "client", serverCts.Token); await Task.WhenAny(responseTask, monitorTask).ConfigureAwait(false); Log("End reading response"); @@ -263,17 +263,13 @@ internal static string GetRuntimeDirectoryOpt() /// The IsConnected property on named pipes does not detect when the client has disconnected /// if we don't attempt any new I/O after the client disconnects. We start an async I/O here /// which serves to check the pipe for disconnection. - /// - /// This will return true if the pipe was disconnected. /// - private static async Task CreateMonitorDisconnectTask( - NamedPipeClientStream pipeStream, - CancellationToken cancellationToken) + internal static async Task CreateMonitorDisconnectTask( + PipeStream pipeStream, + string identifier = null, + CancellationToken cancellationToken = default(CancellationToken)) { - // Ignore this warning because the desktop projects don't target 4.6 yet -#pragma warning disable CA1825 // Avoid zero-length array allocations. - var buffer = new byte[0]; -#pragma warning restore CA1825 // Avoid zero-length array allocations. + var buffer = Array.Empty(); while (!cancellationToken.IsCancellationRequested && pipeStream.IsConnected) { @@ -282,17 +278,18 @@ internal static string GetRuntimeDirectoryOpt() try { - Log("Before poking pipe."); + Log($"Before poking pipe {identifier}."); await pipeStream.ReadAsync(buffer, 0, 0, cancellationToken).ConfigureAwait(false); - Log("After poking pipe."); + Log($"After poking pipe {identifier}."); + } + catch (OperationCanceledException) + { } - // Ignore cancellation - catch (OperationCanceledException) { } catch (Exception e) { // It is okay for this call to fail. Errors will be reflected in the // IsConnected property which will be read on the next iteration of the - LogException(e, "Error poking pipe"); + LogException(e, $"Error poking pipe {identifier}."); } } }