Skip to content

Commit

Permalink
Update internalUpdateFileDescriptor() to synchronize setProto() and r…
Browse files Browse the repository at this point in the history
…esolveAllFeatures() to avoid data races. (#15659)

PiperOrigin-RevId: 601628994
  • Loading branch information
zhangskz authored Jan 31, 2024
1 parent 77efb8d commit 225b080
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions java/core/src/main/java/com/google/protobuf/Descriptors.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,10 @@ public static void internalUpdateFileDescriptor(
ByteString bytes = descriptor.proto.toByteString();
try {
FileDescriptorProto proto = FileDescriptorProto.parseFrom(bytes, registry);
descriptor.setProto(proto);
synchronized (descriptor) {
descriptor.setProto(proto);
descriptor.resolveAllFeatures();
}
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException(
"Failed to parse protocol buffer descriptor for generated code.", e);
Expand Down Expand Up @@ -707,7 +710,6 @@ private void setProto(final FileDescriptorProto proto) {
this.proto = proto;
this.features = null;
this.options = null;
this.features = resolveFeatures(proto.getOptions().getFeatures());

for (int i = 0; i < messageTypes.length; i++) {
messageTypes[i].setProto(proto.getMessageType(i));
Expand Down Expand Up @@ -1161,7 +1163,6 @@ private void setProto(final DescriptorProto proto) {
this.proto = proto;
this.features = null;
this.options = null;
this.features = resolveFeatures(proto.getOptions().getFeatures());

for (int i = 0; i < nestedTypes.length; i++) {
nestedTypes[i].setProto(proto.getNestedType(i));
Expand Down Expand Up @@ -1978,7 +1979,6 @@ private void setProto(final FieldDescriptorProto proto) {
this.proto = proto;
this.features = null;
this.options = null;
this.features = resolveFeatures(proto.getOptions().getFeatures());
}

/** For internal use only. This is to satisfy the FieldDescriptorLite interface. */
Expand Down Expand Up @@ -2259,7 +2259,6 @@ private void setProto(final EnumDescriptorProto proto) {
this.proto = proto;
this.features = null;
this.options = null;
this.features = resolveFeatures(proto.getOptions().getFeatures());

for (int i = 0; i < values.length; i++) {
values[i].setProto(proto.getValue(i));
Expand Down Expand Up @@ -2409,7 +2408,6 @@ private void setProto(final EnumValueDescriptorProto proto) {
this.proto = proto;
this.features = null;
this.options = null;
this.features = resolveFeatures(proto.getOptions().getFeatures());
}
}

Expand Down Expand Up @@ -2535,7 +2533,6 @@ private void setProto(final ServiceDescriptorProto proto) {
this.proto = proto;
this.features = null;
this.options = null;
this.features = resolveFeatures(proto.getOptions().getFeatures());

for (int i = 0; i < methods.length; i++) {
methods[i].setProto(proto.getMethod(i));
Expand Down Expand Up @@ -2686,7 +2683,6 @@ private void setProto(final MethodDescriptorProto proto) {
this.proto = proto;
this.features = null;
this.options = null;
this.features = resolveFeatures(proto.getOptions().getFeatures());
}
}

Expand Down Expand Up @@ -3223,7 +3219,6 @@ private void setProto(final OneofDescriptorProto proto) {
this.proto = proto;
this.features = null;
this.options = null;
this.features = resolveFeatures(proto.getOptions().getFeatures());
}

private OneofDescriptor(
Expand Down

0 comments on commit 225b080

Please sign in to comment.