Skip to content

JsonDocument.GetString implementation is not thread safe #76440

Closed
@eiriktsarpalis

Description

@eiriktsarpalis

Even though JsonDocument is nominally immutable (modulo disposability), the internal GetString implementation employs a caching scheme that is not thread safe:

private (int, string?) _lastIndexAndString = (-1, null);

Since the cached field is a struct that cannot be written to or read from atomically. It might be that we could fix this in the code using a regular System.Tuple, thus ensuring atomic access to cached entries. Obviously this has the downside of incurring one additional allocation, but at least we can guarantee immutability (modulo disposability of course).

Related to #17975

cc @layomia @krwq

Originally posted by @eiriktsarpalis in dotnet/docs#20563 (comment)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions