Skip to content

Fix JsonArray lazy list initialization race - #136136

Merged
eiriktsarpalis merged 2 commits into
dotnet:mainfrom
asylkhan-azat:jsonarray-threading-fix
Oct 22, 2026
Merged

eiriktsarpalis merged 2 commits into
dotnet:mainfrom
asylkhan-azat:jsonarray-threading-fix

Conversation

@asylkhan-azat

@asylkhan-azat asylkhan-azat commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes race on lazy initialization in JsonArray. Copies fix from (JsonObject)

Added tests to catch it too

Resolves #136117

JsonArray.InitializeList() published _list with a plain store, so
threads racing on first access could each build and return their own
list. Losing threads got child nodes whose Parent is the array but
which are not contained in it: GetPath() reports a bogus index and
ReplaceWith() fails to locate the node.

Publish the list with Interlocked.CompareExchange and have losing
threads adopt the winner's list, matching the fix made for JsonObject
in dotnet#120619.

Add tests that access a freshly parsed JsonArray from many threads at
once and verify every thread observes the child instance stored in
the array, with the correct path.

Fixes dotnet#136117
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 20, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.

@asylkhan-azat

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 69ebdec0-7c63-4cbb-b25c-6d2232145ccc
@asylkhan-azat

Copy link
Copy Markdown
Contributor Author

@eiriktsarpalis I noticed that you have deleted the "is thread safe test" for the json array. Should I also kill the similar one that in JsonObject tests since new test covers both? I've copied my one from there

@eiriktsarpalis

Copy link
Copy Markdown
Member

/ba-g test failures unrelated.

@eiriktsarpalis
eiriktsarpalis merged commit 79cff7b into dotnet:main Oct 22, 2026
86 of 88 checks passed
@asylkhan-azat
asylkhan-azat deleted the jsonarray-threading-fix branch October 22, 2026 16:15
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Text.Json community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants