Avoid race condition

Mark default instance as immutable first to avoid race during static initialization of default instances.
pull/10770/head
Jerry Berg 2 years ago committed by GitHub
parent aa3bc05d26
commit 9fb9384516
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java

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

Loading…
Cancel
Save