protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
65.81k stars 15.51k forks source link

Set Ruby C calls as Ractor-safe #19321

Open cretz opened 2 days ago

cretz commented 2 days ago

What language does this apply to?

Ruby

Describe the problem you are trying to solve.

Be able to use Protobuf objects inside of Ruby Ractors.

Describe the solution you'd like

Today, you get things like:

`method_missing': ractor unsafe method called from not main ractor

This is because by default every C extension is considered ractor-unsafe at definition time by default (see https://github.com/ruby/ruby/pull/3824), so Google::Protobuf::AbstractMethod#method_missing as a C method is considered unsafe. They are considered unsafe by default because Ractors are parallel without GVL/GIL and therefore the C extensions must be thread safe within themselves. It appears Protobuf implementation is (not to be confused with general thread safety), so rb_ext_ractor_safe(true); can be called at the top of https://github.com/protocolbuffers/protobuf/blob/74cbd3126f86e346ec68fc3454c757e70dc2eeea/ruby/ext/google/protobuf_c/protobuf.c#L325. This is available in all non-EOL Ruby versions.

Also make a test for creation and use of Protobuf objects inside Ractors (bonus points for making the objects RUBY_TYPED_FROZEN_SHAREABLE and testing they can be made shareable). That way the FFI-based implementation can be confirmed Ractor-safe as well.

Also, assuming Ractor support is easily added for both implementations, would a PR to support this be welcomed? And backported to the 3.x series (but if not that's ok)?