fix: serialize JSONB.parseObject per type to prevent concurrent $ref NPE#16384
Open
mmustafasenoglu wants to merge 2 commits into
Open
fix: serialize JSONB.parseObject per type to prevent concurrent $ref NPE#16384mmustafasenoglu wants to merge 2 commits into
mmustafasenoglu wants to merge 2 commits into
Conversation
When using FastJson2Serialization with references (e.g., shared List.of() singletons serialized by fastjson2), concurrent deserialization under cache-miss conditions produces corrupted ObjectReader instances in fastjson2's ObjectReaderProvider.getObjectReaderInternal (non-atomic check-then-act). This causes $ref resolution to silently fail, leaving fields as null. Add striped locks (64 stripes) keyed by target class to serialize JSONB.parseObject calls per type. Concurrent deserialization of different types is unaffected; only same-type concurrent deserialization is serialized, preventing the race condition. Fixes apache#16368 Signed-off-by: Mustafa Senoglu <mmustafasenoglu0@gmail.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 3.3 #16384 +/- ##
============================================
+ Coverage 60.86% 60.88% +0.01%
- Complexity 11763 11769 +6
============================================
Files 1953 1953
Lines 89262 89272 +10
Branches 13471 13472 +1
============================================
+ Hits 54331 54350 +19
- Misses 29329 29333 +4
+ Partials 5602 5589 -13
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
mmustafasenoglu
added a commit
to mmustafasenoglu/dubbo
that referenced
this pull request
Jul 17, 2026
Add tests for the striped lock mechanism introduced in PR apache#16384: - testReadObjectWithType: exercises readObject(Class, Type) overload - testReadObjectWithTypeList: exercises readObject with ParameterizedType for List - testReadObjectNullClass: exercises getStripe(null) branch - testConcurrentDeserializationSameType: concurrent deserialization of same type (same stripe) - testConcurrentDeserializationDifferentTypes: concurrent deserialization of different types (different stripes) - testReadObjectWithTypeAndString: readObject(Class, Type) with String - testReadObjectWithTypeMap: readObject(Class, Type) with ParameterizedType for Map These tests improve patch coverage from 64.28% by covering: - getStripe() with null cls parameter - readObject(Class<T>, Type) code path - synchronized block execution across multiple threads Signed-off-by: Mustafa Senoglu <mmustafasenoglu0@gmail.com>
Add tests for the striped lock mechanism introduced in this PR: - testReadObjectWithType: exercises readObject(Class, Type) overload - testReadObjectWithTypeList: exercises readObject with ParameterizedType for List - testReadObjectNullClass: exercises getStripe(null) branch - testConcurrentDeserializationSameType: concurrent deserialization of same type (same stripe) - testConcurrentDeserializationDifferentTypes: concurrent deserialization of different types (different stripes) - testReadObjectWithTypeAndString: readObject(Class, Type) with String - testReadObjectWithTypeMap: readObject(Class, Type) with ParameterizedType for Map These tests improve patch coverage by covering: - getStripe() with null cls parameter - readObject(Class<T>, Type) code path - synchronized block execution across multiple threads Signed-off-by: Mustafa Senoglu <mmustafasenoglu0@gmail.com>
Author
|
Hi, added New test methods:
These should cover the previously uncovered |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is the purpose of the change
Fix a race condition in
FastJson2ObjectInputwhere concurrent deserialization of JSONB bytes containing$refreferences produces null fields.Brief changelog
FastJson2ObjectInputkeyed by target classJSONB.parseObjectcalls insynchronized (getStripe(cls))blocksgetStripe(Class<?>)helper methodRelated issue
Fixes #16368
Detailed explanation
Root cause
When multiple objects reference the same shared singleton (e.g.,
List.of()), fastjson2 serialization writes$refreferences instead of duplicating the value. During concurrent deserialization:ObjectReaderProvider.getObjectReaderInternalhas a non-atomic check-then-act:if (objectReader == null) { createObjectReader(...); putIfAbsent(...); }createObjectReadercalls produce corrupted ObjectReader instances (incomplete FieldReader arrays)$refresolution viafieldReader.accept()fails silently → fields remain nullFix
Striped locks keyed by target class serialize
JSONB.parseObjectcalls per type. Concurrent deserialization of different types is unaffected; only same-type concurrent deserialization is serialized, preventing the fastjson2 race condition.The 64-stripe design provides sufficient concurrency for typical applications while bounding memory usage.
How to reproduce
See the reproduction test in the issue: 200 threads with CyclicBarrier, cache cleared each round, ~1% of tasks produce null fields.