Skip to content

JsonArray: concurrent first access can return child nodes that are not in the array #136117

Closed

Description

@asylkhan-azat

Description

JsonArray.InitializeList() publishes _list with a plain store (JsonArray.cs#L294-L325), so threads racing on first access can each build their own list. The losers end up with children that point to the array as Parent but aren't in it, which also breaks ReplaceWith().

Same issue as #120617, which #120619 fixed for JsonObject only.

Reproduction Steps

using System.Text.Json.Nodes;

const int Iterations = 20_000;
const int Threads = 8;

int affectedArrays = 0;
int orphanedChildren = 0;

for (int i = 0; i < Iterations; i++)
{
    var array = (JsonArray)JsonNode.Parse("[1,2,3]")!;
    var children = new JsonNode?[Threads];
    using var go = new ManualResetEventSlim();

    var threads = new Thread[Threads];
    for (int t = 0; t < Threads; t++)
    {
        int index = t;
        threads[t] = new Thread(() =>
        {
            go.Wait();
            children[index] = array[0];
        });
        threads[t].Start();
    }

    go.Set();
    foreach (var thread in threads)
    {
        thread.Join();
    }

    int orphaned = children.Count(c => c!.GetPath() != "$[0]");
    if (orphaned > 0)
    {
        affectedArrays++;
        orphanedChildren += orphaned;
    }
}

Console.WriteLine($"{affectedArrays} of {Iterations} arrays affected, {orphanedChildren} children report a wrong path");

Expected behavior

All threads get the same child node and GetPath() returns $[0], per the documented guarantee that concurrent reads on a JsonArray are safe.

Actual behavior

.NET 10.0.12:          15 of 20000 arrays affected, 36 children report a wrong path
main (f20fde8255):    111 of 20000 arrays affected, 497 children report a wrong path

Regression?

No, present since #77567 (.NET 8).

Known Workarounds

Touch each JsonArray (including nested ones) on a single thread before sharing it.

Configuration

I've run the repro on two configurations:

  • .NET 10.0.12
  • main at f20fde8; Windows 11 x64, Release.

Other information

Proposed fix, same as #120619:

List<JsonNode?>? published = Interlocked.CompareExchange(ref _list, list, null);
if (published is null)
{
    _jsonElement = null;
}
else
{
    list = published;
}

Optionally, in the same change, GetUnderlyingRepresentation (JsonArray, JsonObject) could use Volatile.ReadBarrier() instead of Interlocked.MemoryBarrier() (context):

jsonElement = _jsonElement;
#if NET
Volatile.ReadBarrier();
#else
Interlocked.MemoryBarrier();
#endif
list = _list;

Not a correctness change, and I haven't measured it.
Happy to send a PR for the fix, with or without the barrier change.

Activity

  1. added
    untriaged
    on Oct 20, 2026
  2. dotnet-policy-service commented on Oct 20, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
    See info in area-owners.md if you want to be subscribed.

  3. removed
    untriaged
    on Oct 22, 2026
  4. added a commit that references this issue on Oct 22, 2026
    79cff7b
  5. locked and limited conversation to collaborators on Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions