LitJSON / litjson

JSON library for the .Net framework
https://litjson.net/
Other
1.36k stars 403 forks source link

Race conditions affecting JsonMapper and its usage of static members w/o synchronization #143

Closed udlose closed 9 months ago

udlose commented 1 year ago

There are several members in the JsonMapper class that are static IDictionary<T> instances. A few are only ever written to during static initialization (which is fine) and thus do not require synchronization. However, there are a few which are mutable and cause synchronization errors.

I've experienced these synchronization issues thru AWSXRayRecorder's usage of your library (https://github.com/aws/aws-xray-sdk-dotnet/issues/282). I've opened a PR with a fix - #142

  Message: 
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.

  Stack Trace: 
Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
Dictionary`2.set_Item(TKey key, TValue value)
JsonMapper.RegisterExporter[T](ExporterFunc`1 exporter) line 922
JsonSegmentMarshaller.ctor() line 48
UdpSegmentEmitter.ctor() line 45
AWSXRayRecorder.ctor() line 47
TelemetryRecorderTests.ctor() line 25
RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
udlose commented 1 year ago

I've updated my PR and restored the formatting of the unchanged code. Please review the PR again. https://github.com/LitJSON/litjson/pull/142

devlead commented 9 months ago

Fixed by #142