diff --git a/src/StreamJsonRpc/JsonRpc.cs b/src/StreamJsonRpc/JsonRpc.cs index e4a8d4f7..d45dc6e0 100644 --- a/src/StreamJsonRpc/JsonRpc.cs +++ b/src/StreamJsonRpc/JsonRpc.cs @@ -2387,6 +2387,7 @@ private async Task ReadAndHandleRequestsAsync() try { this.TraceSource.TraceEvent(TraceEventType.Information, (int)TraceEvents.ListeningStarted, "Listening started."); + Exception? loopBreakingException = null; while (!this.IsDisposed && !this.DisconnectedToken.IsCancellationRequested) { @@ -2400,12 +2401,14 @@ private async Task ReadAndHandleRequestsAsync() return; } } - catch (OperationCanceledException) + catch (OperationCanceledException ex) when (this.DisconnectedToken.IsCancellationRequested) { + loopBreakingException = ex; break; } - catch (ObjectDisposedException) + catch (ObjectDisposedException ex) when (this.IsDisposed) { + loopBreakingException = ex; break; } #pragma warning disable CA1031 // Do not catch general exception types @@ -2426,7 +2429,7 @@ private async Task ReadAndHandleRequestsAsync() (this.MessageHandler as IJsonRpcMessageBufferManager)?.DeserializationComplete(protocolMessage); } - this.OnJsonRpcDisconnected(new JsonRpcDisconnectedEventArgs(Resources.StreamDisposed, DisconnectedReason.LocallyDisposed)); + this.OnJsonRpcDisconnected(new JsonRpcDisconnectedEventArgs(Resources.StreamDisposed, DisconnectedReason.LocallyDisposed, loopBreakingException)); } catch (Exception ex) {