fix: serialize JSONB.parseObject per type to prevent concurrent $ref NPE - #16384
fix: serialize JSONB.parseObject per type to prevent concurrent $ref NPE#16384mmustafasenoglu 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 #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:
|
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>
|
Hi, added New test methods:
These should cover the previously uncovered |
There was a problem hiding this comment.
Pull request overview
This PR addresses a concurrency bug in Dubbo’s fastjson2-based deserialization path by serializing JSONB.parseObject per target type using striped locking, aiming to prevent corrupted ObjectReader instances (and ensuing $ref resolution issues) under concurrent cache-miss conditions.
Changes:
- Added 64 striped locks to
FastJson2ObjectInputand wrappedJSONB.parseObjectcalls withsynchronized (getStripe(cls)). - Introduced
getStripe(Class<?>)helper for type-based lock striping. - Added a new JUnit test class intended to exercise concurrent deserialization behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2ObjectInput.java | Adds striped locking around JSONB parsing to mitigate fastjson2 ObjectReader creation races. |
| dubbo-serialization/dubbo-serialization-fastjson2/src/test/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2StripedLockTest.java | Adds concurrency-focused tests for fastjson2 deserialization scenarios. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.CyclicBarrier; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.TimeUnit; |
| private Serialization createSerialization() { | ||
| FrameworkModel frameworkModel = new FrameworkModel(); | ||
| return frameworkModel | ||
| .getExtensionLoader(Serialization.class) | ||
| .getExtension("fastjson2"); | ||
| } | ||
|
|
||
| private URL createURL() { | ||
| FrameworkModel frameworkModel = new FrameworkModel(); | ||
| return URL.valueOf("").setScopeModel(frameworkModel); | ||
| } |
| void testConcurrentDeserializationSameType() throws Exception { | ||
| FrameworkModel frameworkModel = new FrameworkModel(); | ||
| Serialization serialization = | ||
| frameworkModel.getExtensionLoader(Serialization.class).getExtension("fastjson2"); | ||
| URL url = URL.valueOf("").setScopeModel(frameworkModel); | ||
|
|
||
| TrustedPojo pojo = new TrustedPojo(123.0); |
| private static Object getStripe(Class<?> cls) { | ||
| int hash = cls == null ? 0 : cls.getName().hashCode(); | ||
| return STRIPES[(hash & 0x7FFFFFFF) % STRIPE_COUNT]; | ||
| } |
|
The approach that #16369 used might be more elegant |
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.