Skip to content

Commit aea0e52

Browse files
committed
Resolve features directly in setProto instead of temporarily setting to null.
Avoid potential races with other threads reading features that do not share a lock while features are temporarily null. Special handling for proto1 mutable should not actually be needed, since setProto doesn't update dependency protos. PiperOrigin-RevId: 610783483
1 parent f00528d commit aea0e52

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

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

+16-19
Original file line numberDiff line numberDiff line change
@@ -482,20 +482,17 @@ public static FileDescriptor internalBuildGeneratedFileFrom(
482482
return internalBuildGeneratedFileFrom(descriptorDataParts, dependencies);
483483
}
484484

485-
public static void internalUpdateFileDescriptorImmutable(
485+
/**
486+
* This method is to be called by generated code only. It updates the FileDescriptorProto
487+
* associated with the descriptor by parsing it again with the given ExtensionRegistry. This is
488+
* needed to recognize custom options.
489+
*/
490+
public static void internalUpdateFileDescriptor(
486491
FileDescriptor descriptor, ExtensionRegistry registry) {
487-
internalUpdateFileDescriptor(descriptor, registry, false);
488-
}
489-
490-
private static void internalUpdateFileDescriptor(
491-
FileDescriptor descriptor, ExtensionRegistry registry, boolean mutable) {
492492
ByteString bytes = descriptor.proto.toByteString();
493493
try {
494494
FileDescriptorProto proto = FileDescriptorProto.parseFrom(bytes, registry);
495-
synchronized (descriptor) {
496-
descriptor.setProto(proto);
497-
descriptor.resolveAllFeaturesImmutable();
498-
}
495+
descriptor.setProto(proto);
499496
} catch (InvalidProtocolBufferException e) {
500497
throw new IllegalArgumentException(
501498
"Failed to parse protocol buffer descriptor for generated code.", e);
@@ -712,10 +709,10 @@ private void crossLink() throws DescriptorValidationException {
712709
* construct the descriptors we have to have parsed the descriptor protos. So, we have to parse
713710
* the descriptor protos a second time after constructing the descriptors.
714711
*/
715-
private void setProto(final FileDescriptorProto proto) {
712+
private synchronized void setProto(final FileDescriptorProto proto) {
716713
this.proto = proto;
717-
this.features = null;
718714
this.options = null;
715+
this.features = resolveFeatures(proto.getOptions().getFeatures());
719716

720717
for (int i = 0; i < messageTypes.length; i++) {
721718
messageTypes[i].setProto(proto.getMessageType(i));
@@ -1167,8 +1164,8 @@ private void validateNoDuplicateFieldNumbers() throws DescriptorValidationExcept
11671164
/** See {@link FileDescriptor#setProto}. */
11681165
private void setProto(final DescriptorProto proto) {
11691166
this.proto = proto;
1170-
this.features = null;
11711167
this.options = null;
1168+
this.features = resolveFeatures(proto.getOptions().getFeatures());
11721169

11731170
for (int i = 0; i < nestedTypes.length; i++) {
11741171
nestedTypes[i].setProto(proto.getNestedType(i));
@@ -1983,8 +1980,8 @@ private void crossLink() throws DescriptorValidationException {
19831980
/** See {@link FileDescriptor#setProto}. */
19841981
private void setProto(final FieldDescriptorProto proto) {
19851982
this.proto = proto;
1986-
this.features = null;
19871983
this.options = null;
1984+
this.features = resolveFeatures(proto.getOptions().getFeatures());
19881985
}
19891986

19901987
/** For internal use only. This is to satisfy the FieldDescriptorLite interface. */
@@ -2263,8 +2260,8 @@ private void resolveAllFeatures() {
22632260
/** See {@link FileDescriptor#setProto}. */
22642261
private void setProto(final EnumDescriptorProto proto) {
22652262
this.proto = proto;
2266-
this.features = null;
22672263
this.options = null;
2264+
this.features = resolveFeatures(proto.getOptions().getFeatures());
22682265

22692266
for (int i = 0; i < values.length; i++) {
22702267
values[i].setProto(proto.getValue(i));
@@ -2412,8 +2409,8 @@ private void resolveAllFeatures() {
24122409
/** See {@link FileDescriptor#setProto}. */
24132410
private void setProto(final EnumValueDescriptorProto proto) {
24142411
this.proto = proto;
2415-
this.features = null;
24162412
this.options = null;
2413+
this.features = resolveFeatures(proto.getOptions().getFeatures());
24172414
}
24182415
}
24192416

@@ -2537,8 +2534,8 @@ private void crossLink() throws DescriptorValidationException {
25372534
/** See {@link FileDescriptor#setProto}. */
25382535
private void setProto(final ServiceDescriptorProto proto) {
25392536
this.proto = proto;
2540-
this.features = null;
25412537
this.options = null;
2538+
this.features = resolveFeatures(proto.getOptions().getFeatures());
25422539

25432540
for (int i = 0; i < methods.length; i++) {
25442541
methods[i].setProto(proto.getMethod(i));
@@ -2687,8 +2684,8 @@ private void crossLink() throws DescriptorValidationException {
26872684
/** See {@link FileDescriptor#setProto}. */
26882685
private void setProto(final MethodDescriptorProto proto) {
26892686
this.proto = proto;
2690-
this.features = null;
26912687
this.options = null;
2688+
this.features = resolveFeatures(proto.getOptions().getFeatures());
26922689
}
26932690
}
26942691

@@ -3223,8 +3220,8 @@ private void resolveAllFeatures() {
32233220

32243221
private void setProto(final OneofDescriptorProto proto) {
32253222
this.proto = proto;
3226-
this.features = null;
32273223
this.options = null;
3224+
this.features = resolveFeatures(proto.getOptions().getFeatures());
32283225
}
32293226

32303227
private OneofDescriptor(

src/google/protobuf/compiler/java/file.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ void FileGenerator::GenerateDescriptorInitializationCodeForImmutable(
490490
}
491491
printer->Print(
492492
"com.google.protobuf.Descriptors.FileDescriptor\n"
493-
" .internalUpdateFileDescriptorImmutable(descriptor, registry);\n");
493+
" .internalUpdateFileDescriptor(descriptor, registry);\n");
494494
}
495495

496496
printer->Outdent();

0 commit comments

Comments
 (0)