Skip to content

Set Ruby C calls as Ractor-safe #19321

@cretz

Description

@cretz

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 ruby/ruby#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

__attribute__((visibility("default"))) void Init_protobuf_c() {
. 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)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    rubyuntriagedauto added to all issues by default when created.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions