ruby: register grpc_rb_sStatus as a global variable

Ref: https://bugs.ruby-lang.org/issues/20311

C global variable that contain references to Ruby object MUST
be declared to the Ruby GC to prevent these objects from being
collected or moved.

There are a few exceptions to that, such as classes defined using
the C APIs such as `rb_define_class` etc.

Up to Ruby 3.4 however, there was a bug that caused classes created
from Ruby with the `Struct.new("Name")` API to also be marked as
immortal and immovable.

GRPC has been relying on this bug, which I fixed in Ruby 3.4, and
now GRPC is crashing when `Struct::Status` is moved around by the GC.
pull/36125/head
Jean Boussier 10 months ago
parent db51a3f69a
commit 7a127599c8
  1. 1
      src/ruby/ext/grpc/rb_grpc.c

@ -467,6 +467,7 @@ void Init_grpc_c() {
grpc_rb_mGrpcCore = rb_define_module_under(grpc_rb_mGRPC, "Core");
grpc_rb_sNewServerRpc = rb_struct_define(
"NewServerRpc", "method", "host", "deadline", "metadata", "call", NULL);
rb_global_variable(&grpc_rb_sStatus);
grpc_rb_sStatus = rb_const_get(rb_cStruct, rb_intern("Status"));
sym_code = ID2SYM(rb_intern("code"));
sym_details = ID2SYM(rb_intern("details"));

Loading…
Cancel
Save