Skip to content

Commit 1594ac1

Browse files
committed
Merge pull request #10770 from protocolbuffers/googleberg-cl-480629524
Mark default instance as immutable first to avoid race during static initialization of default instances.
1 parent 5d90ef2 commit 1594ac1

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,13 @@ Object buildMessageInfo() throws Exception {
407407

408408
protected static <T extends GeneratedMessageLite<?, ?>> void registerDefaultInstance(
409409
Class<T> clazz, T defaultInstance) {
410+
// Default instances must be immutable.
411+
// Marking immutable here to avoid extra bytecode in every generated message class.
412+
// Only calling "markImmutable" rather than "makeImmutable" because for Default Instances:
413+
// 1. All sub-messages are initialized to null / default instances and thus immutable
414+
// 2. All lists are initialized to default instance empty lists which are also immutable.
415+
defaultInstance.markImmutable();
410416
defaultInstanceMap.put(clazz, defaultInstance);
411-
defaultInstance.makeImmutable();
412417
}
413418

414419
protected static Object newMessageInfo(

0 commit comments

Comments
 (0)