Skip to content

Commit fbecd3f

Browse files
Backport: Catch stream exceptions for some Debug Adapter stability. (#1022) (#1026)
* catch stream exceptions for Debug Adapter * Use WriteHandledException
1 parent c79ccf5 commit fbecd3f

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/PowerShellEditorServices.Protocol/MessageProtocol/MessageWriter.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public async Task WriteMessage(Message messageToWrite)
6262
// the log level is Diagnostic where JsonRpc message payloads are logged and vary
6363
// in size from 1K up to 225K chars. When not logging message payloads, the typical
6464
// response log message size is under 256 chars.
65-
var logStrBld =
65+
var logStrBld =
6666
new StringBuilder(this.logger.MinimumConfiguredLogLevel == LogLevel.Diagnostic ? 4096 : 256)
6767
.Append("Writing ")
6868
.Append(messageToWrite.MessageType)
@@ -76,7 +76,7 @@ public async Task WriteMessage(Message messageToWrite)
7676
if (this.logger.MinimumConfiguredLogLevel == LogLevel.Diagnostic)
7777
{
7878
// Log the JSON representation of the message payload at the Diagnostic log level
79-
string jsonPayload =
79+
string jsonPayload =
8080
JsonConvert.SerializeObject(
8181
messageObject,
8282
Formatting.Indented,
@@ -104,10 +104,20 @@ public async Task WriteMessage(Message messageToWrite)
104104
// message loop doesn't get blocked while waiting for I/O to complete.
105105
using (await this.writeLock.LockAsync())
106106
{
107-
// Send the message
108-
await this.outputStream.WriteAsync(headerBytes, 0, headerBytes.Length);
109-
await this.outputStream.WriteAsync(messageBytes, 0, messageBytes.Length);
110-
await this.outputStream.FlushAsync();
107+
try
108+
{
109+
// Send the message
110+
await this.outputStream.WriteAsync(headerBytes, 0, headerBytes.Length);
111+
await this.outputStream.WriteAsync(messageBytes, 0, messageBytes.Length);
112+
await this.outputStream.FlushAsync();
113+
}
114+
catch (Exception e) when (
115+
e is ObjectDisposedException ||
116+
e is IOException)
117+
{
118+
// We catch this exception for when the DebugAdapter disconnects while still processing a message.
119+
logger.WriteHandledException("Tried to write to the output stream but it was already closed & broken.", e);
120+
}
111121
}
112122
}
113123

0 commit comments

Comments
 (0)