Skip to content

Commit 2df0be6

Browse files
author
Martin Lercher
committed
BugFix - fixed OperationCanceledException in OutputHandlerTest
1 parent 63b4b19 commit 2df0be6

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/JsonRpc/OutputHandler.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public OutputHandler(TextWriter output)
1919
_output = output;
2020
_queue = new BlockingCollection<object>();
2121
_cancel = new CancellationTokenSource();
22-
_thread = new Thread(ProcessOutputQueue) {
22+
_thread = new Thread(ProcessOutputQueue)
23+
{
2324
IsBackground = true
2425
};
2526
}
@@ -37,11 +38,11 @@ public void Send(object value)
3738
private void ProcessOutputQueue()
3839
{
3940
var token = _cancel.Token;
40-
while (true)
41+
try
4142
{
42-
if (_thread == null) return;
43-
try
43+
while (true)
4444
{
45+
if (_thread == null) return;
4546
if (_queue.TryTake(out var value, Timeout.Infinite, token))
4647
{
4748
var content = JsonConvert.SerializeObject(value);
@@ -55,8 +56,14 @@ private void ProcessOutputQueue()
5556
_output.Write(sb.ToString());
5657
}
5758
}
58-
catch (OperationCanceledException) { }
5959
}
60+
catch (OperationCanceledException ex)
61+
{
62+
if (ex.CancellationToken != token)
63+
throw;
64+
// else ignore. Exceptions: OperationCanceledException - The CancellationToken has been canceled.
65+
}
66+
finally { _cancel.Dispose(); }
6067
}
6168

6269
public void Dispose()

0 commit comments

Comments
 (0)