json-flatfile-datastore icon indicating copy to clipboard operation
json-flatfile-datastore copied to clipboard

System.OperationCanceledException When Disposing DataStore

Open pvoelker opened this issue 3 years ago • 3 comments

I'm using your library to implement a set of local data stores. I love this library and the implementation. I am seeing a behavior I am trying to resolve as I am implementing unhandled exception handling now in the my .NET 6 WPF application. So this keeps tripping it...

Here is an example of one of my methods on one of the repositories:

        public IEnumerable<QuestionSet> GetAll()
        {
            using (var ds = new DataStore(_fileName, true, nameof(QuestionSet.PrimaryKey)))
            {
                var coll = ds.GetCollection<QuestionSet>();
                return coll.AsQueryable().ToList();
            }
        }

After the 'using' exits I always get a: System.OperationCanceledException

If I don't use 'using' or do not Dispose, I don't see the exception.

Should I not be wrapping the DataStore object in a using? Or am I using the library in the wrong way?

pvoelker avatar Jul 01 '22 14:07 pvoelker

Hi! Nice to hear that you have found use for the library :)

The recommended way to use DataStore is to create a single instance for the whole application lifecycle. You would only need to dispose the datastore when you close the application.

Nevertheless, I will inspect if I find the reason for the System.OperationCanceledException.

ttu avatar Jul 02 '22 06:07 ttu

Thank you for the hint about the DataStore. I will implement a singleton to hold a DataStore instance.

Looking at the code it looks like this may call the exception to be thrown?: https://github.com/ttu/json-flatfile-datastore/blob/24ad435fe834c9aaba6c64bfbab7bff53c48ef82/JsonFlatFileDataStore/DataStore.cs#L145

Looking at this though (https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-cancellation) it seems that should only occur if something awaiting on the task? Have not done much work with cancellation token before....

pvoelker avatar Jul 02 '22 23:07 pvoelker

Thanks for pointing me forward. Yes, CancellationToken is then the reason for this Exception.

It is only used with BlockingCollection in the background process, which executes the store updates and updates the JSON file. https://github.com/ttu/json-flatfile-datastore/blob/24ad435fe834c9aaba6c64bfbab7bff53c48ef82/JsonFlatFileDataStore/DataStore.cs#L86

Apparently, at least some of the BlockingCollection methods throw that exception https://stackoverflow.com/questions/8953407/using-blockingcollectiont-operationcanceledexception-is-there-a-better-way

I will leave this Issue open and inspect this topic more.

ttu avatar Jul 03 '22 04:07 ttu

I'm getting this a lot too, every time I'm closing it, actually. It's a singleton, but I load a different file based on what's needed. But I'm manually calling Dispose before instantiating it again, maybe that's not the right way to do it?

edit: it's also being done from a separate thread

nabeelio avatar Dec 30 '22 20:12 nabeelio

Thanks for the comment! That should be the right way if you need to change the file based on your need. What .NET version are you using and are you developing for web or desktop?

This might be a fix for this: https://github.com/ttu/json-flatfile-datastore/pull/78

ttu avatar Jan 04 '23 21:01 ttu

Hi @ttu sorry for the delayed response, just getting back to this. Thanks for the fix! I stopped calling Dispose() manually and that ended up fixing it too, but maybe there were still some side-effects which are fixed. I'll upgrade the library today! Thanks again!

nabeelio avatar Jan 10 '23 14:01 nabeelio

@nabeelio Good that you found a workaround👍

New version with the fix in now released https://www.nuget.org/packages/JsonFlatFileDataStore/2.4.1

ttu avatar Jan 15 '23 08:01 ttu