ruby / timeout

Timeout provides a way to auto-terminate a potentially long-running operation if it hasn't finished in a fixed amount of time.
Other
145 stars 23 forks source link

incompatible with ractors #53

Open HoneyryderChuck opened 2 weeks ago

HoneyryderChuck commented 2 weeks ago

currently, timeout can't be used in ractors:

Ractor.new do
  Timeout.timeout(4) do
    sleep(5)
  end
end.take
# .rubies/ruby-3.3.0/lib/ruby/3.3.0/timeout.rb:129:in `ensure_timeout_thread_created': can not access non-shareable objects in # # constant Timeout::TIMEOUT_THREAD_MUTEX by non-main ractor. (Ractor::IsolationError)
#         from .rubies/ruby-3.3.0/lib/ruby/3.3.0/timeout.rb:178:in `timeout'
#         from (irb):8:in `block in <top (required)>'

Lots of downstream dependencies rely on timeout, and this makes them unusable under ractors.

eregon commented 2 weeks ago

I think Thread in general is considered incompatible with Ractor (it breaks the programming model and there are several bugs with Ractor & threads), and so Timeout (which needs threads) is incompatible with Ractor.

Also: https://bugs.ruby-lang.org/issues/18139#note-2

HoneyryderChuck commented 2 weeks ago

I wouldn't go so far. If timeout would use ractor-local thread/queue/cond/mutex, it'd work. A completely alternative ractor-friendly implementation could also be an option (although I can't think of one). Was just pointing out that, the way it's implemented, it's unusable.