Actions
Feature #20805
closedAllow Ractor#send from a signal trap Proc
Status:
Feedback
Assignee:
-
Target version:
-
Description
It was surfaced in https://bugs.ruby-lang.org/issues/18139 that Ractor is designed to "unexpected interruption free" and it's not supposed to have a kill
-like method.
A better alternative to that would be allow the main Ractor to consume a shutdown signal from the pipe.
Example:
shutdown_ractor = Ractor.new do
Ractor.recv # Wait for a signal
end
# Signal trap for SIGTERM
Signal.trap("TERM") do
puts "Received SIGTERM, shutting down..."
shutdown_ractor.send(:shutdown) # Send the shutdown signal
end
pipe = Ractor.new do
loop do
r, value = Ractor.select(Ractor.recv, shutdown_ractor)
if r == shutdown_ractor # Shutdown signal received
break
else
Ractor.yield(value, move: true) # Normal operation
end
end
end
However, it's not possible to do Ractor#send
from a trap Proc right now:
<internal:ractor>:282:in `new': can not isolate a Proc because it accesses outer variables (shutdown_ractor). (ArgumentError)
From what I've gathered, it makes it impossible to implement any graceful shutdown in a process with a few Ractors running.
Updated by alanwu (Alan Wu) 29 days ago
It works if you make shutdown_ractor
a constant:
p Process.pid
SHUTDOWN_RACTOR = Ractor.new do
Ractor.recv # Wait for a signal
puts "goodbye"
exit!
end
# Signal trap for SIGTERM
Signal.trap("TERM") do
puts "Received SIGTERM, shutting down..."
SHUTDOWN_RACTOR.send(:shutdown) # Send the shutdown signal
end
sleep
ruby -v test.rb
ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [arm64-darwin24]
79270
test.rb:3: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.
Received SIGTERM, shutting down...
goodbye
Not to minimize the usability issue you experienced.
Actions
Like0
Like0Like0