From be73b996e2c69b327fa8904dc43f72561586d50a Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 27 Mar 2024 20:58:03 +0100 Subject: [PATCH 1/3] remove synchronization from ProtobufMapper --- .../dataformat/protobuf/ProtobufMapper.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 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..0a62d4b7b 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; @@ -21,6 +22,8 @@ public class ProtobufMapper extends ObjectMapper { private static final long serialVersionUID = 1L; + private final ReentrantLock _lock = new ReentrantLock(); + /** * Base implementation for "Vanilla" {@link ObjectMapper}, used with * Protobuf backend. @@ -42,7 +45,7 @@ public Builder(ProtobufMapper m) { * * @since 2.9 */ - protected DescriptorLoader _descriptorLoader; + protected volatile DescriptorLoader _descriptorLoader; /* /********************************************************** @@ -192,11 +195,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); + _lock.lock(); + try { + l = _descriptorLoader; + if (l == null) { + l = _descriptorLoader = DescriptorLoader.construct(this); + } + } finally { + _lock.unlock(); + } } return l; } From 57699ee2aa4cf2e062c493da06dc0e0385006a15 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 27 Mar 2024 21:01:35 +0100 Subject: [PATCH 2/3] Update ProtobufMapper.java --- .../fasterxml/jackson/dataformat/protobuf/ProtobufMapper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0a62d4b7b..71fedddab 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 @@ -203,7 +203,7 @@ public DescriptorLoader descriptorLoader() throws IOException try { l = _descriptorLoader; if (l == null) { - l = _descriptorLoader = DescriptorLoader.construct(this); + _descriptorLoader = l = DescriptorLoader.construct(this); } } finally { _lock.unlock(); From dbe79e9be65ced6b664e04af6e6bec26e51a2ac3 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Sun, 7 Apr 2024 12:52:27 -0700 Subject: [PATCH 3/3] Add release notes, rename lock --- .../jackson/dataformat/protobuf/ProtobufMapper.java | 9 +++++---- release-notes/CREDITS-2.x | 4 ++++ release-notes/VERSION-2.x | 3 ++- 3 files changed, 11 insertions(+), 5 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 71fedddab..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 @@ -22,8 +22,6 @@ public class ProtobufMapper extends ObjectMapper { private static final long serialVersionUID = 1L; - private final ReentrantLock _lock = new ReentrantLock(); - /** * Base implementation for "Vanilla" {@link ObjectMapper}, used with * Protobuf backend. @@ -47,6 +45,9 @@ public Builder(ProtobufMapper m) { */ protected volatile DescriptorLoader _descriptorLoader; + // @since 2.18 + private final ReentrantLock _descriptorLock = new ReentrantLock(); + /* /********************************************************** /* Life-cycle @@ -199,14 +200,14 @@ public DescriptorLoader descriptorLoader() throws IOException { DescriptorLoader l = _descriptorLoader; if (l == null) { - _lock.lock(); + _descriptorLock.lock(); try { l = _descriptorLoader; if (l == null) { _descriptorLoader = l = DescriptorLoader.construct(this); } } finally { - _lock.unlock(); + _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)