From 11e4b1ff3ca4df321704edcf8014449a293dc6d1 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sun, 7 Apr 2024 21:54:23 +0200 Subject: [PATCH] Rework synchronization in `ProtobufMapper` (#484) --- .../dataformat/protobuf/ProtobufMapper.java | 18 +++++++++++++++--- release-notes/CREDITS-2.x | 4 ++++ release-notes/VERSION-2.x | 3 ++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufMapper.java b/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufMapper.java index a32a092a5..c78a3f7b1 100644 --- a/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufMapper.java +++ b/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufMapper.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.util.concurrent.locks.ReentrantLock; import com.fasterxml.jackson.core.Version; import com.fasterxml.jackson.core.type.TypeReference; @@ -42,7 +43,10 @@ public Builder(ProtobufMapper m) { * * @since 2.9 */ - protected DescriptorLoader _descriptorLoader; + protected volatile DescriptorLoader _descriptorLoader; + + // @since 2.18 + private final ReentrantLock _descriptorLock = new ReentrantLock(); /* /********************************************************** @@ -192,11 +196,19 @@ public FileDescriptorSet loadDescriptorSet(InputStream src) throws IOException { * * @since 2.9 */ - public synchronized DescriptorLoader descriptorLoader() throws IOException + public DescriptorLoader descriptorLoader() throws IOException { DescriptorLoader l = _descriptorLoader; if (l == null) { - _descriptorLoader = l = DescriptorLoader.construct(this); + _descriptorLock.lock(); + try { + l = _descriptorLoader; + if (l == null) { + _descriptorLoader = l = DescriptorLoader.construct(this); + } + } finally { + _descriptorLock.unlock(); + } } return l; } diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 38da6d77b..fb51155ad 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -319,3 +319,7 @@ Thomas de Lange (@thomasdelange5) * Contributed fix for #428: (ion) `IonParser.getIntValue()` fails or does not handle value overflow checks (2.17.0) + +PJ Fanning (pjfanning@github) + * Contributed #484: Rework synchronization in `ProtobufMapper` + (2.18.0) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 9a33fac2e..977e0cfef 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -16,7 +16,8 @@ Active maintainers: 2.18.0 (not yet released) -No changes since 2.17. +#484: (protobuf) Rework synchronization in `ProtobufMapper` + (contributed by @pjfanning) 2.17.0 (12-Mar-2024)