Open-NET-Libraries / Open.ChannelExtensions

A set of extensions for optimizing/simplifying System.Threading.Channels usage.
https://open-net-libraries.github.io/Open.ChannelExtensions/api/Open.ChannelExtensions.Extensions.html#methods
MIT License
401 stars 25 forks source link

BatchingChannelReader Timer ObjectDisposedException #20

Closed Lukazoid closed 2 years ago

Lukazoid commented 2 years ago

Today I encountered the following exception:

System.ObjectDisposedException: Cannot access a disposed object.
   at System.Threading.TimerQueueTimer.Change(UInt32 dueTime, UInt32 period)
   at System.Threading.Timer.Change(Int64 dueTime, Int64 period)
   at Open.ChannelExtensions.BatchingChannelReader`1.TryPipeItems(Boolean flush)
   at Open.ChannelExtensions.BufferingChannelReader`2.TryRead(TOut& item)

I think this can occur because it's possible TryPipeItems retrieves the _timer value on thread A, the last item is pushed to the channel on thread B which disposes and nulls _timer, but then TryPipeItems on thread A still tries to use the value it previously loaded which has now been disposed.

electricessence commented 2 years ago

Investigating.

electricessence commented 2 years ago

I believe you are correct about the possibility. I'm attempting a fix. Will be published shortly.

electricessence commented 2 years ago

v6.0.3 released to nuget https://www.nuget.org/packages/Open.ChannelExtensions/6.0.3

THANK YOU!!!