Skip to content

Catch stream exceptions for some Debug Adapter stability. #1022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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.WriteHandledException("Tried to write to the output stream but it was already closed & broken.", e);
}
}
}

Expand Down