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 1 commit
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 @@ -110,7 +110,7 @@ final class DeserBean<T> {

private final Map<String, Argument<?>> bounds;

private volatile boolean initialized;
volatile boolean initialized;
private volatile boolean initializing;

// CHECKSTYLE:ON
Expand Down Expand Up @@ -427,16 +427,11 @@ public AnnotationMetadata getAnnotationMetadata() {
}

void initialize(Deserializer.DecoderContext decoderContext) throws SerdeException {
// Double check locking
if (!initialized) {
synchronized (this) {
if (!initialized && !initializing) {
initializing = true;
initializeInternal(decoderContext);
initialized = true;
initializing = false;
}
}
if (!initialized && !initializing) {
initializing = true;
initializeInternal(decoderContext);
initialized = true;
initializing = false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ 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);
// Double check locking
if (!deserBean.initialized) {
synchronized (this) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe switch to reentrantlock

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For virtual threads?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls check now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, also makes it more clear that initialize needs a lock.

deserBean.initialize(decoderContext);
}
}
return (DeserBean<T>) deserBean;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ private <T> SerBean<T> getSerializableBean(Argument<T> type,
}
}));
SerBean<?> serBean = serBeanSupplier.get();
serBean.initialize(context);
// Double check locking
if (!serBean.initialized) {
synchronized (this) {
serBean.initialize(context);
}
}
return (SerBean<T>) serBean;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public int getOrder() {
@Nullable
private final SerdeArgumentConf serdeArgumentConf;

private volatile boolean initialized;
volatile boolean initialized;
private volatile boolean initializing;

private List<Initializer> initializers = new ArrayList<>();
Expand Down Expand Up @@ -377,19 +377,15 @@ private static PropertySubtypeDescriptor findDescriptor(@Nullable SubtypeInfo su
}
}

public void initialize(Serializer.EncoderContext encoderContext) throws SerdeException {
if (!initialized) {
synchronized (this) {
if (!initialized && !initializing) {
initializing = true;
for (Initializer initializer : initializers) {
initializer.initialize(encoderContext);
}
initializers = null;
initialized = true;
initializing = false;
}
void initialize(Serializer.EncoderContext encoderContext) throws SerdeException {
if (!initialized && !initializing) {
initializing = true;
for (Initializer initializer : initializers) {
initializer.initialize(encoderContext);
}
initializers = null;
initialized = true;
initializing = false;
}
}

Expand Down
Loading