Skip to content

Commit ce9a1c6

Browse files
Refactor BatchingChannelReader timeout handling and improve test reliability
1 parent c38fbbd commit ce9a1c6

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

Open.ChannelExtensions.Tests/HangReproTest.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ public static async Task MultipleBatchReadersDoNotHang()
2626
await GetSource()
2727
.ToChannel(singleReader: true, cancellationToken: token)
2828
.Batch(Random.Shared.Next(15, 30), singleReader: true)
29-
.ReadAllAsync(async batch =>
30-
{
31-
await Task.Delay(Random.Shared.Next(2, 15), token);
32-
}, token);
29+
.WithTimeout(TimeSpan.FromMilliseconds(100))
30+
.ReadAllAsync(async batch => await Task.Delay(Random.Shared.Next(2, 15), token), token);
3331

3432
counts[x] += 1;
3533
}

Open.ChannelExtensions/Readers/BatchingChannelReader.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,14 @@ public BatchingChannelReader<T, TBatch> WithTimeout(long millisecondsTimeout)
9595

9696
private void TryUpdateTimer(long timeout)
9797
{
98+
var t = _timer;;
99+
if (t is null) return;
100+
98101
try
99102
{
100-
bool? ok = _timer?.Change(timeout, 0);
101-
Debug.Assert(ok ?? true);
103+
t.Change(timeout, 0);
104+
// bool? ok = t.Change(timeout, 0);
105+
// Debug.Assert(ok ?? true);
102106
}
103107
catch (ObjectDisposedException)
104108
{

0 commit comments

Comments
 (0)