Skip to content

Conversation

@jar-stripe
Copy link
Contributor

Why?

SerializablePropertyCache (used for System.Text.Json serialization) caches properties and converter instances in a pair of Dictionary objects. #3100 reported a a concurrency related error when they initiated two simultaneous calls to a method that made an API call and serialized the results on return using STJ. This PR fixes this issue by switching the Dictionary objects to thread-safe alternatives and refactoring the cache-building code to also be thread-safe.

What?

  • Replaced Dictionary objects with ConcurrentDictionary objects in SerializablePropertyCache
  • refactored where we populate the dictionaries to use GetOrAdd, which provides thread-safe access to the value or a callback if the key/value is not set yet.

See Also

@jar-stripe jar-stripe marked this pull request as ready for review April 11, 2025 22:08
@jar-stripe jar-stripe requested a review from a team as a code owner April 11, 2025 22:08
@jar-stripe jar-stripe requested review from mbroshi-stripe and removed request for a team April 11, 2025 22:08
@jar-stripe jar-stripe enabled auto-merge (squash) April 15, 2025 01:03
@jar-stripe jar-stripe merged commit b277911 into master Apr 15, 2025
4 checks passed
@jar-stripe jar-stripe deleted the jar/fix-stj-property-cache-concurrency branch April 15, 2025 01:09
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

Successfully merging this pull request may close these issues.

4 participants