Improve exception diagnostics for stdio client (#376)

* Improve exception diagnostics for stdio client

Track the last several lines of stderr and include those in an exception created as part of CleanupAsync if the server process has already exited. This is based on an assumption that the server should never exit prior to CleanupAsync being called.

* Address feedback and fix test failure
This commit is contained in:
Stephen Toub 2025-05-14 10:46:49 -04:00 • committed by GitHub
commit c11c634945
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -51,7 +51,7 @@ public class StreamServerTransport : TransportBase
_inputReader = new StreamReader(inputStream, Encoding.UTF8);
_outputStream = outputStream;
SetConnected(true);
SetConnected();
_readLoopCompleted = Task.Run(ReadMessagesAsync, _shutdownCts.Token);
}
@ -60,7 +60,7 @@ public class StreamServerTransport : TransportBase
{
if (!IsConnected)
{
throw new InvalidOperationException("Transport is not connected");
return;
}
using var _ = await _sendLock.LockAsync(cancellationToken).ConfigureAwait(false);
@ -80,13 +80,14 @@ public class StreamServerTransport : TransportBase
catch (Exception ex)
{
LogTransportSendFailed(Name, id, ex);
throw new InvalidOperationException("Failed to send message", ex);
throw new IOException("Failed to send message.", ex);
}
}
private async Task ReadMessagesAsync()
{
CancellationToken shutdownToken = _shutdownCts.Token;
Exception? error = null;
try
{
LogTransportEnteringReadMessagesLoop(Name);
@ -140,10 +141,11 @@ public class StreamServerTransport : TransportBase
catch (Exception ex)
{
LogTransportReadMessagesFailed(Name, ex);
error = ex;
}
finally
{
SetConnected(false);
SetDisconnected(error);
}
}
@ -183,7 +185,7 @@ public class StreamServerTransport : TransportBase
}
finally
{
SetConnected(false);
SetDisconnected();
LogTransportShutDown(Name);
}