Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is JsonTransformer thread-safe? #270

Open
joshidp opened this issue May 2, 2023 · 8 comments
Open

Is JsonTransformer thread-safe? #270

joshidp opened this issue May 2, 2023 · 8 comments

Comments

@joshidp
Copy link

joshidp commented May 2, 2023

Hello JUST.net,

I am using JsonTransformer with single instance and when in multi-thread environment I am doing transform then results are not expected. Below code fails when jsonTransformer is declared outside the parallel loop but it works if jsonTransformer is declared inside loop context. How are we suppose to use JsonTransformer ? is it thread-safe?

var jsonTransformer = new JsonTransformer();

            var data = new List<THreadTestData>();

            data.Add(new THreadTestData()
            {
                Transformer = File.ReadAllText("atomicTX.json"),
                Input = File.ReadAllText("atomic.json"),
                Expected = "{\r\n  \"alias\": \"abc\",\r\n  \"eou\": null,\r\n  \"resourceType\": null,\r\n  \"resourceCategory\": \"FFF\",  \"reportsTo\": \"pqr\"\r\n}"
            });

            data.Add(new THreadTestData()
            {
                Transformer = File.ReadAllText("bulkTX.json"),
                Input = File.ReadAllText("bulk.json"),
                Expected = "[\r\n  {\r\n    \"alias\": \"abc\"\r\n  },\r\n  {\r\n    \"alias\": \"xyz\"\r\n  }\r\n]"
            });

            // Execute the loop in parallel
            Parallel.For(0, 100, i =>
            {
                THreadTestData threadTestData = null;
                {
                    threadTestData = data.First();
                    // var jsonTransformer = new JsonTransformer();
                    var tx = jsonTransformer.Transform(JObject.Parse(threadTestData.Transformer), threadTestData.Input);

                    if (tx.ToString() != threadTestData.Expected)
                    {

                    }
                    else
                    {

                    }
                }
                {
                    threadTestData = data.Last();
                    //  var jsonTransformer = new JsonTransformer();
                    var tx = jsonTransformer.Transform(JObject.Parse(threadTestData.Transformer), threadTestData.Input);

                    if (tx.ToString() != threadTestData.Expected)
                    {

                    }
                }


                Task.Delay(2 * 100).Wait();
            });

Thanks

@Courela
Copy link
Contributor

Courela commented May 2, 2023

Current version should be thread-safe.
#260

@joshidp
Copy link
Author

joshidp commented May 3, 2023

I am using latest 4.3.0 and its not acting thread-safe, above code snippet scenario can validate it.

@Courela
Copy link
Contributor

Courela commented May 3, 2023

You're right, different instances of JsonTransformer work fine, but the same instance does not, there's a property, JUSTContext, that is read and set throughout the code...

@joshidp
Copy link
Author

joshidp commented May 3, 2023

JUSTContext have setting values, transformer and inputs are not part of it, so how JUSTContext can help here? Is it a bug?

JUSTContext context = new JUSTContext 
{ 
  EvaluationMode = EvaluationMode.Strict,
  DefaultDecimalPlaces = 4
};

@Courela
Copy link
Contributor

Courela commented May 3, 2023

Input is a property of JUSTContext (internal), it is set at the beginning of the tranformation, and it is changed whenever is needed (for example, multiple transformations use it, I'm not sure but maybe #applyover and loops also...)

@joshidp
Copy link
Author

joshidp commented May 4, 2023

So, sounds like JsonTransformer is not thread-safe in true sense and we should re-instantiate JsonTransformer for each transformation.

@Courela
Copy link
Contributor

Courela commented May 4, 2023

Not thread-safe at all... One thing to check

@s-summers
Copy link

Are there any plans to release this fix to nuget?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants