-
-
Notifications
You must be signed in to change notification settings - Fork 78
Description
Hello again :)
I've been experimenting with Ractors since upgrading to Ruby 3.0 for #275 but quickly ran into Ractor::UnsafeError when trying to call Ox::sax_parse in anything except the main Ractor.
Per Ruby's Ractor C extension documentation (link below):
"By default, all C extensions are recognized as Ractor-unsafe. If C extension becomes Ractor-safe, the extension should call rb_ext_ractor_safe(true) at the Init_ function and all defined method marked as Ractor-safe. Ractor-unsafe C-methods only been called from main-ractor. If non-main ractor calls it, then Ractor::UnsafeError is raised."
I don't like to open seemingly-large feature requests like this without making some attempt at it myself first,
and luckily it seems like Ox::sax_parse Just Works™ since I marked it Ractor-safe, even with the class_cache. Confirming this safety, making any remaining changes to Ox::Sax, and expanding this to the non-Sax parts of Ox are all unfortunately out of my depth as a n00b C coder, so I would appreciate if you could take this over if it interests you. I am happy with just Sax support since I have no current need for marshalling, but I imagine other Ox users wouldn't be satisfied if stratified. I have a PR incoming for this change.
Official Ractor info:
- "Ractor: a proposal for a new concurrent abstraction without thread-safety issues": https://bugs.ruby-lang.org/issues/17100 (Ractor: a proposal for new concurrent abstraction without thread-safety issues ruby/ruby#3365)
- Ruby's official
Ractordocumentation: https://docs.ruby-lang.org/en/master/doc/ractor_md.html - "A way to mark C extensions as thread-safe, Ractor-safe, or unsafe": https://bugs.ruby-lang.org/issues/17307 (rb_ext_ractor_safe() to declare ractor-safe ext ruby/ruby#3824)
- Ruby's C Extension
Ractordocumention coveringrb_ext_ractor_safe: https://docs.ruby-lang.org/en/master/doc/extension_rdoc.html#label-Appendix+F.+Ractor+support - A
RactorC Extension from the creator ofRactorthat might serve as a useful example: https://github.com/ko1/ractor-tvar
Blogs:
- "Ractors: Multi-Core Parallel Processing Comes to Ruby 3": https://www.ruby3.dev/ruby-3-fundamentals/2021/01/27/ractors-multi-core-parallel-processing-in-ruby-3/
- "Ruby Ractor Experiments: Safe async communication" :https://ivoanjo.me/blog/2021/02/14/ractor-experiments-safe-async/
- "Playing with Ruby Ractors": https://billy-ruffian.co.uk/playing-with-ruby-ractors/
- "How Fast are Ractors?": https://www.fastruby.io/blog/ruby/performance/how-fast-are-ractors.html (https://github.com/noahgibbs/ractor_basic_benchmarks/tree/main/benchmarks)