Skip to content

Commit 352b4ce

Browse files
Dispose timer fix.
1 parent 8612fa3 commit 352b4ce

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Open.ChannelExtensions/BatchingChannelReader.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,19 @@ public BatchingChannelReader<T> WithTimeout(long millisecondsTimeout)
8686
/// If one exists, updates the timer's timeout value.
8787
/// </summary>
8888
[MethodImpl(MethodImplOptions.AggressiveInlining)]
89-
protected void RefreshTimeout()
89+
protected void RefreshTimeout() => TryUpdateTimer(_timeout);
90+
91+
private void TryUpdateTimer(long timeout)
9092
{
91-
var ok = _timer?.Change(_timeout, 0);
92-
Debug.Assert(ok ?? true);
93+
try
94+
{
95+
var ok = _timer?.Change(timeout, 0);
96+
Debug.Assert(ok ?? true);
97+
}
98+
catch(ObjectDisposedException)
99+
{
100+
// Rare instance where another thread has disposed the timer before .Change can be called.
101+
}
93102
}
94103

95104
/// <param name="timeout">
@@ -173,7 +182,7 @@ void Emit(ref List<T>? c)
173182
{
174183
_batch = null;
175184
newBatch = false;
176-
if (!batched) _timer?.Change(Timeout.Infinite, 0); // Since we're emmitting one, let's ensure the timeout is cancelled.
185+
if (!batched) TryUpdateTimer(Timeout.Infinite); // Since we're emmitting one, let's ensure the timeout is cancelled.
177186
batched = Buffer!.Writer.TryWrite(c!);
178187
Debug.Assert(batched);
179188
c = null;

0 commit comments

Comments
 (0)