StephenCleary / AsyncEx

A helper library for async/await.
MIT License
3.49k stars 358 forks source link

The collection has been marked as complete with regards to additions #268

Open mikasoukhov opened 1 year ago

mikasoukhov commented 1 year ago

Sometimes while using AsyncContext.Run I've got the following

System.InvalidOperationException HResult=0x80131509 Message=The collection has been marked as complete with regards to additions. Source= StackTrace:

> System.dll!System.Collections.Concurrent.BlockingCollection>.TryAddWithNoTimeValidation(System.Tuple item, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken) Unknown Nito.AsyncEx.Context.dll!Nito.AsyncEx.AsyncContext.TaskQueue.TryAdd(System.Threading.Tasks.Task item, bool propagateExceptions) Unknown Nito.AsyncEx.Context.dll!Nito.AsyncEx.AsyncContext.Enqueue(System.Threading.Tasks.Task task, bool propagateExceptions) Unknown mscorlib.dll!System.Threading.Tasks.TaskContinuation.InlineIfPossibleOrElseQueue(System.Threading.Tasks.Task task, bool needsProtection) Unknown mscorlib.dll!System.Threading.Tasks.Task.ContinueWithCore(System.Threading.Tasks.Task continuationTask, System.Threading.Tasks.TaskScheduler scheduler, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions options) Unknown mscorlib.dll!System.Threading.Tasks.Task.ContinueWith(System.Action continuationAction, System.Threading.Tasks.TaskScheduler scheduler, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, ref System.Threading.StackCrawlMark stackMark) Unknown mscorlib.dll!System.Threading.Tasks.Task.ContinueWith(System.Action continuationAction, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler) Unknown Nito.AsyncEx.Context.dll!Nito.AsyncEx.AsyncContext.Enqueue(System.Threading.Tasks.Task task, bool propagateExceptions) Unknown mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunCallback(System.Threading.ContextCallback callback, object state, ref System.Threading.Tasks.Task currentTask) Unknown

In my case I use AsyncContext.Run for several methonds and the same time and I believe the error causes by following line https://github.com/StephenCleary/AsyncEx/blob/8a73d0467d40ca41f9f9cf827c7a35702243abb8/src/Nito.AsyncEx.Context/AsyncContext.cs#L76

Don't know why need to close underlying collection in case of empty tasks. Like my errors shows a newly may comes just a little bit later.

StephenCleary commented 1 year ago

Are you using fire-and-forget from within an AsyncContext.Run? Can you post sample code that reproduces this issue?