From 17ca3a6644d7de0b70f79eb0c30b7190967b81fa Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Fri, 6 Sep 2019 12:06:16 -0700 Subject: [PATCH 1/2] catch stream exceptions for Debug Adapter --- .../MessageProtocol/MessageWriter.cs | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/MessageProtocol/MessageWriter.cs b/src/PowerShellEditorServices.Protocol/MessageProtocol/MessageWriter.cs index 5baa40038..5e83c54cf 100644 --- a/src/PowerShellEditorServices.Protocol/MessageProtocol/MessageWriter.cs +++ b/src/PowerShellEditorServices.Protocol/MessageProtocol/MessageWriter.cs @@ -62,7 +62,7 @@ public async Task WriteMessageAsync(Message messageToWrite) // the log level is Diagnostic where JsonRpc message payloads are logged and vary // in size from 1K up to 225K chars. When not logging message payloads, the typical // response log message size is under 256 chars. - var logStrBld = + var logStrBld = new StringBuilder(this.logger.MinimumConfiguredLogLevel == LogLevel.Diagnostic ? 4096 : 256) .Append("Writing ") .Append(messageToWrite.MessageType) @@ -76,7 +76,7 @@ public async Task WriteMessageAsync(Message messageToWrite) if (this.logger.MinimumConfiguredLogLevel == LogLevel.Diagnostic) { // Log the JSON representation of the message payload at the Diagnostic log level - string jsonPayload = + string jsonPayload = JsonConvert.SerializeObject( messageObject, Formatting.Indented, @@ -104,10 +104,20 @@ public async Task WriteMessageAsync(Message messageToWrite) // message loop doesn't get blocked while waiting for I/O to complete. using (await this.writeLock.LockAsync()) { - // Send the message - await this.outputStream.WriteAsync(headerBytes, 0, headerBytes.Length); - await this.outputStream.WriteAsync(messageBytes, 0, messageBytes.Length); - await this.outputStream.FlushAsync(); + try + { + // Send the message + await this.outputStream.WriteAsync(headerBytes, 0, headerBytes.Length); + await this.outputStream.WriteAsync(messageBytes, 0, messageBytes.Length); + await this.outputStream.FlushAsync(); + } + catch (Exception e) when ( + e is ObjectDisposedException || + e is IOException) + { + // We catch this exception for when the DebugAdapter disconnects while still processing a message. + logger.WriteException("Tried to write to the output stream but it was already closed & broken.", e); + } } } From adb25f5aa7bf879ea0a504958c19d578f652f9c2 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Tue, 10 Sep 2019 10:11:15 -0700 Subject: [PATCH 2/2] Use WriteHandledException --- .../MessageProtocol/MessageWriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices.Protocol/MessageProtocol/MessageWriter.cs b/src/PowerShellEditorServices.Protocol/MessageProtocol/MessageWriter.cs index 5e83c54cf..17c7da384 100644 --- a/src/PowerShellEditorServices.Protocol/MessageProtocol/MessageWriter.cs +++ b/src/PowerShellEditorServices.Protocol/MessageProtocol/MessageWriter.cs @@ -116,7 +116,7 @@ e is ObjectDisposedException || e is IOException) { // We catch this exception for when the DebugAdapter disconnects while still processing a message. - logger.WriteException("Tried to write to the output stream but it was already closed & broken.", e); + logger.WriteHandledException("Tried to write to the output stream but it was already closed & broken.", e); } } }