Skip to content

Commit a101c9e

Browse files
committed
Temporary fix for high CPU usage when stream lost
This change introduces a "fix" for the issue where the language service host process will spin at high CPU if the client's input stream terminates unexpectedly. This issue is caused by incorrectly handling the result of Stream.ReadAsync which states that a result of 0 means that the stream has terminated. Instead of returning true and continuing the read loop, I now throw an EndOfStreamException which causes the app to terminate with an unhandled exception. A future fix will be implemented to provide a more graceful shutdown path.
1 parent dd1e4f2 commit a101c9e

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/PowerShellEditorServices.Protocol/MessageProtocol/MessageReader.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,18 @@ await this.inputStream.ReadAsync(
142142

143143
this.bufferEndOffset += readLength;
144144

145-
return readLength >= 0;
145+
if (readLength == 0)
146+
{
147+
// If ReadAsync returns 0 then it means that the stream was
148+
// closed unexpectedly (usually due to the client application
149+
// ending suddenly). For now, just terminate the language
150+
// server immediately.
151+
// TODO: Provide a more graceful shutdown path
152+
throw new EndOfStreamException(
153+
"MessageReader's input stream ended unexpectedly, terminating.");
154+
}
155+
156+
return true;
146157
}
147158

148159
private bool TryReadMessageHeaders()

0 commit comments

Comments
 (0)