ohler55 / ox

Ruby Optimized XML Parser
http://www.ohler.com/ox
MIT License
904 stars 76 forks source link

Become `Ractor`-safe to allow usage in non-main `Ractor`. #277

Open okeeblow opened 3 years ago

okeeblow commented 3 years ago

Hello again :)

I've been experimenting with Ractors since upgrading to Ruby 3.0 for https://github.com/ohler55/ox/issues/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:

Blogs:

ohler55 commented 3 years ago

I'll need to do some reading on Ractor to make sure Ox is indeed actor safe then the addition of the call is easy. Just one place.