Skip to content

Commit d0cd01b

Browse files
authored
Revert "Fix #4561 (revert #4430)" -- i.e. go back to modified #4430 for 2.17.2 (#4568)
1 parent b89886a commit d0cd01b

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/DeserializerCache.java

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.databind.deser;
22

33
import java.util.HashMap;
4+
import java.util.concurrent.locks.ReentrantLock;
45

56
import com.fasterxml.jackson.annotation.JsonFormat;
67
import com.fasterxml.jackson.databind.*;
@@ -52,6 +53,15 @@ public final class DeserializerCache
5253
protected final HashMap<JavaType, JsonDeserializer<Object>> _incompleteDeserializers
5354
= new HashMap<JavaType, JsonDeserializer<Object>>(8);
5455

56+
57+
/**
58+
* We hold an explicit lock while creating deserializers to avoid creating duplicates.
59+
* Guards {@link #_incompleteDeserializers}.
60+
*
61+
* @since 2.17
62+
*/
63+
private final ReentrantLock _incompleteDeserializersLock = new ReentrantLock();
64+
5565
/*
5666
/**********************************************************
5767
/* Life-cycle
@@ -162,10 +172,9 @@ public JsonDeserializer<Object> findValueDeserializer(DeserializationContext ctx
162172
// If not, need to request factory to construct (or recycle)
163173
deser = _createAndCacheValueDeserializer(ctxt, factory, propertyType);
164174
if (deser == null) {
165-
/* Should we let caller handle it? Let's have a helper method
166-
* decide it; can throw an exception, or return a valid
167-
* deserializer
168-
*/
175+
// Should we let caller handle it? Let's have a helper method
176+
// decide it; can throw an exception, or return a valid
177+
// deserializer
169178
deser = _handleUnknownValueDeserializer(ctxt, propertyType);
170179
}
171180
}
@@ -204,9 +213,8 @@ public boolean hasValueDeserializerFor(DeserializationContext ctxt,
204213
DeserializerFactory factory, JavaType type)
205214
throws JsonMappingException
206215
{
207-
/* Note: mostly copied from findValueDeserializer, except for
208-
* handling of unknown types
209-
*/
216+
// Note: mostly copied from findValueDeserializer, except for
217+
// handling of unknown types
210218
JsonDeserializer<Object> deser = _findCachedDeserializer(type);
211219
if (deser == null) {
212220
deser = _createAndCacheValueDeserializer(ctxt, factory, type);
@@ -245,7 +253,8 @@ protected JsonDeserializer<Object> _createAndCacheValueDeserializer(Deserializat
245253
// Only one thread to construct deserializers at any given point in time;
246254
// limitations necessary to ensure that only completely initialized ones
247255
// are visible and used.
248-
synchronized (_incompleteDeserializers) {
256+
_incompleteDeserializersLock.lock();
257+
try {
249258
// Ok, then: could it be that due to a race condition, deserializer can now be found?
250259
JsonDeserializer<Object> deser = _findCachedDeserializer(type);
251260
if (deser != null) {
@@ -268,6 +277,8 @@ protected JsonDeserializer<Object> _createAndCacheValueDeserializer(Deserializat
268277
_incompleteDeserializers.clear();
269278
}
270279
}
280+
} finally {
281+
_incompleteDeserializersLock.unlock();
271282
}
272283
}
273284

0 commit comments

Comments
 (0)