Skip to content

Commit 2dd06c6

Browse files
ntkmecopybara-github
authored andcommitted
Fix jruby encoding in new thread (#12919)
In jruby `runtime.evalScriptlet('Encoding::UTF_8')` can be broken under a newly created thread, which can cause protobuf encoding to fail. For example: https://github.com/ntkme/sass-embedded-host-ruby/actions/runs/5108602231/jobs/9182569583#step:5:544 It real issue seems to be in jruby itself. A bug report with minimal reproduction has been filed at jruby/jruby#7820 This PR fixes the problem by effectively replacing `runtime.evalScriptlet('Encoding::UTF_8')` with `runtime.getEncodingService().convertEncodingToRubyEncoding(org.jcodings.specific.UTF8Encoding.INSTANCE)`, which works on all threads. Closes #12919 COPYBARA_INTEGRATE_REVIEW=#12919 from ntkme:fix-jruby-encoding 345680e FUTURE_COPYBARA_INTEGRATE_REVIEW=#12919 from ntkme:fix-jruby-encoding 345680e PiperOrigin-RevId: 536498592
1 parent 4b3615c commit 2dd06c6

File tree

1 file changed

+10
-4
lines changed
  • ruby/src/main/java/com/google/protobuf/jruby

1 file changed

+10
-4
lines changed

ruby/src/main/java/com/google/protobuf/jruby/Utils.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto;
3737
import com.google.protobuf.Descriptors.FieldDescriptor;
3838
import java.math.BigInteger;
39+
import org.jcodings.Encoding;
3940
import org.jcodings.specific.ASCIIEncoding;
41+
import org.jcodings.specific.UTF8Encoding;
4042
import org.jruby.*;
4143
import org.jruby.exceptions.RaiseException;
4244
import org.jruby.ext.bigdecimal.RubyBigDecimal;
@@ -141,12 +143,12 @@ public static IRubyObject checkType(
141143
throw createInvalidTypeError(context, "boolean", fieldName, value);
142144
break;
143145
case BYTES:
144-
value = validateAndEncodeString(context, "bytes", fieldName, value, "Encoding::ASCII_8BIT");
146+
value = validateAndEncodeString(context, "bytes", fieldName, value, ASCIIEncoding.INSTANCE);
145147
break;
146148
case STRING:
147149
value =
148150
validateAndEncodeString(
149-
context, "string", fieldName, symToString(value), "Encoding::UTF_8");
151+
context, "string", fieldName, symToString(value), UTF8Encoding.INSTANCE);
150152
break;
151153
case MESSAGE:
152154
if (value.getMetaClass() != typeClass) {
@@ -383,11 +385,15 @@ private static IRubyObject validateAndEncodeString(
383385
String fieldType,
384386
String fieldName,
385387
IRubyObject value,
386-
String encoding) {
388+
Encoding encoding) {
387389
if (!(value instanceof RubyString))
388390
throw createInvalidTypeError(context, fieldType, fieldName, value);
389391

390-
value = ((RubyString) value).encode(context, context.runtime.evalScriptlet(encoding));
392+
value =
393+
((RubyString) value)
394+
.encode(
395+
context,
396+
context.runtime.getEncodingService().convertEncodingToRubyEncoding(encoding));
391397
value.setFrozen(true);
392398
return value;
393399
}

0 commit comments

Comments
 (0)