Skip to content

Commit 11e4b1f

Browse files
authored
Rework synchronization in ProtobufMapper (#484)
1 parent b83b16b commit 11e4b1f

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufMapper.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.IOException;
55
import java.io.InputStream;
66
import java.net.URL;
7+
import java.util.concurrent.locks.ReentrantLock;
78

89
import com.fasterxml.jackson.core.Version;
910
import com.fasterxml.jackson.core.type.TypeReference;
@@ -42,7 +43,10 @@ public Builder(ProtobufMapper m) {
4243
*
4344
* @since 2.9
4445
*/
45-
protected DescriptorLoader _descriptorLoader;
46+
protected volatile DescriptorLoader _descriptorLoader;
47+
48+
// @since 2.18
49+
private final ReentrantLock _descriptorLock = new ReentrantLock();
4650

4751
/*
4852
/**********************************************************
@@ -192,11 +196,19 @@ public FileDescriptorSet loadDescriptorSet(InputStream src) throws IOException {
192196
*
193197
* @since 2.9
194198
*/
195-
public synchronized DescriptorLoader descriptorLoader() throws IOException
199+
public DescriptorLoader descriptorLoader() throws IOException
196200
{
197201
DescriptorLoader l = _descriptorLoader;
198202
if (l == null) {
199-
_descriptorLoader = l = DescriptorLoader.construct(this);
203+
_descriptorLock.lock();
204+
try {
205+
l = _descriptorLoader;
206+
if (l == null) {
207+
_descriptorLoader = l = DescriptorLoader.construct(this);
208+
}
209+
} finally {
210+
_descriptorLock.unlock();
211+
}
200212
}
201213
return l;
202214
}

release-notes/CREDITS-2.x

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,7 @@ Thomas de Lange (@thomasdelange5)
319319
* Contributed fix for #428: (ion) `IonParser.getIntValue()` fails or does not handle
320320
value overflow checks
321321
(2.17.0)
322+
323+
PJ Fanning (pjfanning@github)
324+
* Contributed #484: Rework synchronization in `ProtobufMapper`
325+
(2.18.0)

release-notes/VERSION-2.x

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Active maintainers:
1616

1717
2.18.0 (not yet released)
1818

19-
No changes since 2.17.
19+
#484: (protobuf) Rework synchronization in `ProtobufMapper`
20+
(contributed by @pjfanning)
2021

2122
2.17.0 (12-Mar-2024)
2223

0 commit comments

Comments
 (0)