Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use one lock per serialization / deserialization beans to prevent deadlock #872

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiConsumer;
import java.util.function.Predicate;

Expand Down Expand Up @@ -426,16 +427,19 @@ public AnnotationMetadata getAnnotationMetadata() {
recordLikeBean = isRecordLikeBean();
}

void initialize(Deserializer.DecoderContext decoderContext) throws SerdeException {
void initialize(ReentrantLock lock, Deserializer.DecoderContext decoderContext) throws SerdeException {
// Double check locking
if (!initialized) {
synchronized (this) {
lock.lock();
try {
if (!initialized && !initializing) {
initializing = true;
initializeInternal(decoderContext);
initialized = true;
initializing = false;
}
} finally {
lock.unlock();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;

/**
Expand All @@ -51,6 +52,8 @@ public class ObjectDeserializer implements CustomizableDeserializer<Object>, Des
@Nullable
private final SerdeDeserializationPreInstantiateCallback preInstantiateCallback;

private final ReentrantLock lock = new ReentrantLock();

public ObjectDeserializer(SerdeIntrospections introspections,
DeserializationConfiguration deserializationConfiguration,
SerdeConfiguration serdeConfiguration,
Expand Down Expand Up @@ -155,7 +158,7 @@ public <T> DeserBean<T> getDeserializableBean(Argument<T> type, DecoderContext d
// Use suppliers to prevent recursive update because the lambda can call the same method again
Supplier<DeserBean<?>> deserBeanSupplier = deserBeanMap.computeIfAbsent(key, ignore -> SupplierUtil.memoizedNonEmpty(() -> createDeserBean(type, serdeArgumentConf, decoderContext)));
DeserBean<?> deserBean = deserBeanSupplier.get();
deserBean.initialize(decoderContext);
deserBean.initialize(lock, decoderContext);
return (DeserBean<T>) deserBean;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;

/**
Expand All @@ -59,6 +60,8 @@ public final class ObjectSerializer implements CustomizableSerializer<Object> {
@Nullable
private final BeanContext beanContext;

private final ReentrantLock lock = new ReentrantLock();

public ObjectSerializer(SerdeIntrospections introspections,
SerdeConfiguration serdeConfiguration,
SerializationConfiguration serializationConfiguration) {
Expand Down Expand Up @@ -162,7 +165,7 @@ private <T> SerBean<T> getSerializableBean(Argument<T> type,
}
}));
SerBean<?> serBean = serBeanSupplier.get();
serBean.initialize(context);
serBean.initialize(lock, context);
return (SerBean<T>) serBean;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import java.util.function.Predicate;

Expand Down Expand Up @@ -377,9 +378,11 @@ private static PropertySubtypeDescriptor findDescriptor(@Nullable SubtypeInfo su
}
}

public void initialize(Serializer.EncoderContext encoderContext) throws SerdeException {
public void initialize(ReentrantLock lock, Serializer.EncoderContext encoderContext) throws SerdeException {
// Double check locking
if (!initialized) {
synchronized (this) {
lock.lock();
try {
if (!initialized && !initializing) {
initializing = true;
for (Initializer initializer : initializers) {
Expand All @@ -389,6 +392,8 @@ public void initialize(Serializer.EncoderContext encoderContext) throws SerdeExc
initialized = true;
initializing = false;
}
} finally {
lock.unlock();
}
}
}
Expand Down
Loading