microsoft / ClearScript

A library for adding scripting to .NET applications. Supports V8 (Windows, Linux, macOS) and JScript/VBScript (Windows).
https://microsoft.github.io/ClearScript/
MIT License
1.74k stars 148 forks source link

Add ConcurrentPropertyBag #531

Closed irineu closed 11 months ago

irineu commented 1 year ago

Hi, i'm trying the ClearScript solution in a specific scenario with concurrency. I've got an exception and i didn't found a solution on ClearScript docs for solve it:

System.InvalidOperationException: Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct. at System.Collections.Generic.Dictionary2.TryInsert(TKey key, TValue value, InsertionBehavior behavior) at Microsoft.ClearScript.PropertyBag.Add(String key, Object value)

The error is in: Microsoft.ClearScript.PropertyBag.Add, and after look the source code of the PropertyBag class (https://github.com/microsoft/ClearScript/blob/ebd71aaf9e198b549a2e4dcdd8b61b21fefd7841/ClearScript/PropertyBag.cs#L36C9-L36C64), replacing the Dictionary to ConcurrentDictionary may solve this problem... But it will be nice if the ClearScript have an built in solution for deal with concurrency...

ClearScriptLib commented 1 year ago

Hello, @irineu,

We'll add a ConcurrentPropertyBag class in the next release. In the meantime, you can use a custom IPropertyBag implementation. Subclassing PropertyBag could be another option, depending on your exact requirements.

Thanks for the suggestion!

ClearScriptLib commented 11 months ago

Version 7.4.4 added ConcurrentPropertyBag,