-
Notifications
You must be signed in to change notification settings - Fork 15.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set Ruby C calls as Ractor-safe #19321
Comments
I'm not sure that our code is Ractor-safe at the moment. We use a global object cache to map C pointers to Ruby objects:
We would need to remove this, and carefully audit all of our other code, before we could claim to be Ractor-safe. |
Eek, true, maybe same issue with the I have a new project with a use case to use Protobuf objects in Ractors. Can you give some insight into the expected future of the |
Are ffi-based libraries necessarily Ractor-safe? What if they are accessing shared state? Our ffi implementation also uses a global object cache: https://github.com/protocolbuffers/protobuf/blob/main/ruby/lib/google/protobuf/ffi/object_cache.rb#L28 I think it would take some careful work to be Ractor-safe. It would probably be difficult for us to prioritize this work. How widely-used is Ractor? I believe this is the first feature request we've gotten about it. |
I admit I am not familiar enough with
Not very widely at all, and it's technically still experimental (though it has been around for many years). It does not surprise me that this is the first feature request for it. Not being able to prioritize Ractor support is totally justifiable. Can leave this issue open for if/when that occurs and I may be able to look into alternatives for now (for me, I will probably want to avoid the pure Ruby library, but since I'm using a Rust C extension in my project anyways, I may be able to expose |
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:
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), sorb_ext_ractor_safe(true);
can be called at the top ofprotobuf/ruby/ext/google/protobuf_c/protobuf.c
Line 325 in 74cbd31
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)?
The text was updated successfully, but these errors were encountered: