Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method · added in Ruby 4.0

Ractor#monitor

[T < ::Symbol] (::Ractor::Port[T]) -> untyped
ractor.monitor(port) → true or false

Registers the port as a monitoring port for this ractor. When the ractor terminates, the port receives an Array naming the ractor and what happened to it, so that several ractors can report to one port.

  • [ractor, :exited] if the ractor terminated without an unhandled exception.

  • [ractor, :aborted] if it terminated by one.

The Array is built for the receiving ractor, so watching many ractors does not leave shareable objects behind.

Returns true if the monitor was registered (the ractor is still running). Returns false if the ractor had already terminated; in that case the termination message is sent to the port immediately.

Example
r = Ractor.new{ some_task() }
r.monitor(port = Ractor::Port.new)
port.receive #=> [r, :exited]

r = Ractor.new{ raise "foo" }
r.monitor(port = Ractor::Port.new)
port.receive #=> [r, :aborted]

Ruby version changes

  1. 4.0
See also #join #name #inspect #receive also in 4.0