Ractor - Ruby’s Actor-like concurrent abstraction¶↑
Ractor is designed to provide a parallel execution feature of Ruby without thread-safety concerns.
Summary¶↑
Multiple Ractors in an interpreter process¶↑
You can make multiple Ractors and they run in parallel.
Ractor.new{ expr }creates a newRactorandexpris run in parallel on a parallel computer.Interpreter invokes with the first
Ractor(calledmain Ractor).If the main
Ractorterminates, all other Ractors receive termination requests, similar to how threads behave. (if main thread (first invokedThread), Ruby interpreter sends all running threads to terminate execution).Each
Ractorcontains one or more Threads.Threads within the same
Ractorshare a Ractor-wide global lock like GIL (GVL in MRI terminology), so they can’t run in parallel (without releasing GVL explicitly in C-level). Threads in different ractors run in parallel.The overhead of creating a
Ractoris similar to overhead of oneThreadcreation.
Limited sharing between multiple ractors¶↑
Ractors don’t share everything, unlike threads.
Most objects areUnshareable objects, so you don’t need to care about thread-safety problems which are caused by sharing.
Some objects areShareable objects.
Immutable objects: frozen objects which don’t refer to unshareable-objects.
i = 123:iis an immutable object.s = "str".freeze:sis an immutable object.a = [1, [2], 3].freeze:ais not an immutable object becausearefers unshareable-object[2](which is not frozen).h = {c: Object}.freeze:his an immutable object becausehrefersSymbol:cand shareableObjectclass object which is not frozen.
Class/Module objects
Special shareable objects
Ractorobject itself.And more…
Communication between Ractors withRactor::Port¶↑
Ractors communicate with each other and synchronize the execution by message exchanging between Ractors.Ractor::Port is provided for this communication.
port =Ractor::Port.newRactor.newportdo|port|# Other ractors can send to the portport<<42endport.receive# get a message to the port. Only the creator Ractor can receive from the port#=> 42
Ractors have its own default port andRactor#send,Ractor.receive will use it.
Copy & Move semantics to send messages¶↑
To send unshareable objects as messages, objects are copied or moved.
Copy: use deep-copy.
Move: move membership.
Sender can not access the moved object after moving the object.
Guarantee that at least only 1
Ractorcan access the object.
Thread-safety¶↑
Ractor helps to write a thread-safe concurrent program, but we can make thread-unsafe programs with Ractors.
GOOD: Sharing limitation
Most objects are unshareable, so we can’t make data-racy and race-conditional programs.
Shareable objects are protected by an interpreter or locking mechanism.
BAD: Class/Module can violate this assumption
To make it compatible with old behavior, classes and modules can introduce data-race and so on.
Ruby programmers should take care if they modify class/module objects on multi
Ractorprograms.BAD:
Ractorcan’t solve all thread-safety problemsThere are several blocking operations (waiting send) so you can make a program which has dead-lock and live-lock issues.
Some kind of shareable objects can introduce transactions (STM, for example). However, misusing transactions will generate inconsistent state.
WithoutRactor, we need to trace all state-mutations to debug thread-safety issues. WithRactor, you can concentrate on suspicious code which are shared with Ractors.
Creation and termination¶↑
Ractor.new¶↑
Ractor.new{ expr }generates anotherRactor.
# Ractor.new with a block creates new Ractorr =Ractor.newdo# This block will be run in parallel with other ractorsend# You can name a Ractor with `name:` argument.r =Ractor.newname:'test-name'doend# and Ractor#name returns its name.r.name#=> 'test-name'
Given block isolation¶↑
TheRactor executes givenexpr in a given block. Given block will be isolated from outer scope by theProc#isolate method (not exposed yet for Ruby users). To prevent sharing unshareable objects between ractors, block outer-variables,self and other information are isolated.
Proc#isolate is called atRactor creation time (whenRactor.new is called). If givenProc object is not able to isolate because of outer variables and so on, an error will be raised.
begina =truer =Ractor.newdoa#=> ArgumentError because this block accesses `a`.endr.join# see laterrescueArgumentErrorend
The
selfof the given block is theRactorobject itself.
r =Ractor.newdopself.class#=> Ractorself.object_idendr.value==self.object_id#=> false
Passed arguments toRactor.new() becomes block parameters for the given block. However, an interpreter does not pass the parameter object references, but send them as messages (see below for details).
r =Ractor.new'ok'do|msg|msg#=> 'ok'endr.value#=> 'ok'
# almost similar to the last exampler =Ractor.newdomsg =Ractor.receivemsgendr.send'ok'r.value#=> 'ok'
An execution result of given block¶↑
Return value of the given block becomes an outgoing message (see below for details).
r =Ractor.newdo'ok'endr.value#=> `ok`
Error in the given block will be propagated to the receiver of an outgoing message.
r =Ractor.newdoraise'ok'# exception will be transferred to the receiverendbeginr.valuerescueRactor::RemoteError=>ee.cause.class#=> RuntimeErrore.cause.message#=> 'ok'e.ractor#=> rend
Communication between Ractors¶↑
Communication between Ractors is achieved by sending and receiving messages. There are two ways to communicate with each other.
(1) Message sending/receiving via
Ractor::Port(2) Using shareable container objects
Ractor::TVar gem (ko1/ractor-tvar)
more?
Users can control program execution timing with (1), but should not control with (2) (only manage as critical section).
For message sending and receiving, there are two types of APIs: push type and pull type.
(1) send/receive via
Ractor::Port.Ractor::Port#send(obj)(Ractor::Port#<<(obj)is an alias) send a message to the port. Ports are connected to the infinite size incoming queue soRactor::Port#sendwill never block.Ractor::Port#receivedequeue a message from its own incoming queue. If the incoming queue is empty,Ractor::Port#receivecalling will block the execution of a thread.Ractor.select()can wait for the success ofRactor::Port#receive.You can close
Ractor::PortbyRactor::Port#closeonly by the creatorRactorof the port.If the port is closed, you can’t
sendto the port. IfRactor::Port#receiveis blocked for the closed port, then it will raise an exception.When a
Ractoris terminated, the Ractor’s ports are closed.There are 3 ways to send an object as a message
(1) Send a reference: Sending a shareable object, send only a reference to the object (fast)
(2) Copy an object: Sending an unshareable object by copying an object deeply (slow). Note that you can not send an object which does not support deep copy. Some
T_DATAobjects (objects whose class is defined in a C extension, such asStringIO) are not supported.(3) Move an object: Sending an unshareable object reference with a membership. Sender
Ractorcan not access moved objects anymore (raise an exception) after moving it. Current implementation makes new object as a moved object for receiverRactorand copies references of sending object to moved object.T_DATAobjects are not supported.You can choose “Copy” and “Move” by the
move:keyword,Ractor#send(obj, move: true/false)andRactor.yield(obj, move: true/false)(default isfalse(COPY)).
Wait for multiple Ractors withRactor.select¶↑
You can wait multipleRactor port’s receiving. The return value ofRactor.select() is[port, msg] whereport is a ready port andmsg is received message.
To make convenient,Ractor.select can also accept Ractors to wait the termination of Ractors. The return value ofRactor.select() is[r, msg] wherer is a terminatedRactor andmsg is the value of Ractor’s block.
Wait for a single ractor (same asRactor#value):
r1 =Ractor.new{'r1'}r,obj =Ractor.select(r1)r==r1andobj=='r1'#=> true
Waiting for two ractors:
r1 =Ractor.new{'r1'}r2 =Ractor.new{'r2'}rs = [r1,r2]as = []# Wait for r1 or r2's Ractor.yieldr,obj =Ractor.select(*rs)rs.delete(r)as<<obj# Second try (rs only contain not-closed ractors)r,obj =Ractor.select(*rs)rs.delete(r)as<<objas.sort== ['r1','r2']#=> true
TODO: CurrentRactor.select() has the same issue ofselect(2), so this interface should be refined.
TODO:select syntax of go-language uses round-robin technique to make fair scheduling. NowRactor.select() doesn’t use it.
Closing Ractor’s ports¶↑
Ractor::Port#closeclose the ports (similar toQueue#close).port.send(obj)whereportis closed, will raise an exception.When the queue connected to the port is empty and port is closed,
Ractor::Port#receiveraises an exception. If the queue is not empty, it dequeues an object without exceptions.When a
Ractorterminates, the ports are closed automatically.
Example (try to get a result from closedRactor):
r =Ractor.newdo'finish'endr.join# success (wait for the termination)r.value# success (will return 'finish')# the first Ractor which success the `Ractor#value` can get the resultRactor.newrdo|r|r.value#=> Ractor::Errorend
Example (try to send to closed (terminated)Ractor):
r =Ractor.newdoendr.join# wait terminatebeginr.send(1)rescueRactor::ClosedError'ok'else'ng'end
Send a message by copying¶↑
Ractor::Port#send(obj) copyobj deeply ifobj is an unshareable object.
obj ='str'.dupr =Ractor.newobjdo|msg|# return received msg's object_idmsg.object_idendobj.object_id==r.value#=> false
Some objects are not supported to copy the value, and raise an exception.
obj =Thread.new{}beginRactor.newobjdo|msg|msgendrescueTypeError=>ee.message#=> #<TypeError: allocator undefined for Thread>else'ng'# unreachable hereend
Send a message by moving¶↑
Ractor::Port#send(obj, move: true) movesobj to the destinationRactor. If the sourceRactor touches the moved object (for example, call the method likeobj.foo()), it will be an error.
# move with Ractor#sendr =Ractor.newdoobj =Ractor.receiveobj<<' world'endstr ='hello'r.sendstr,move:truemodified =r.value#=> 'hello world'# str is moved, and accessing str from this Ractor is prohibitedbegin# Error because it touches moved str.str<<' exception'# raise Ractor::MovedErrorrescueRactor::MovedErrormodified#=> 'hello world'elseraise'unreachable'end
Some objects are not supported to move, and an exception will be raised.
r =Ractor.newdoRactor.receiveendr.send(Thread.new{},move:true)#=> allocator undefined for Thread (TypeError)
To achieve the access prohibition for moved objects,class replacement technique is used to implement it.
Shareable objects¶↑
The following objects are shareable.
Immutable objects
Small integers, some symbols,
true,false,nil(a.k.a.SPECIAL_CONST_P()objects in internal)Frozen native objects
Frozen
StringandRegexpobjects (their instance variables should refer only shareable objects)Class, Module objects (
T_CLASS,T_MODULEandT_ICLASSin internal)Ractorand other special objects which care about synchronization.
Implementation: Now shareable objects (RVALUE) haveFL_SHAREABLE flag. This flag can be added lazily.
To make shareable objects,Ractor.make_shareable(obj) method is provided. In this case, try to make shareable by freezingobj and recursively traversable objects. This method acceptscopy: keyword (default value is false).Ractor.make_shareable(obj, copy: true) tries to make a deep copy ofobj and make the copied object shareable.
Language changes to isolate unshareable objects between Ractors¶↑
To isolate unshareable objects between Ractors, we introduced additional language semantics on multi-Ractor Ruby programs.
Note that without using Ractors, these additional semantics is not needed (100% compatible with Ruby 2).
Global variables¶↑
Only the mainRactor (aRactor created at starting of interpreter) can access global variables.
$gv =1r =Ractor.newdo$gvendbeginr.joinrescueRactor::RemoteError=>ee.cause.message#=> 'can not access global variables from non-main Ractors'end
Note that some special global variables, such as$stdin,$stdout and$stderr are Ractor-local. See[Bug #17268] for more details.
Instance variables of shareable objects¶↑
Instance variables of classes/modules can be get from non-main Ractors if the referring values are shareable objects.
classC@iv =1endpRactor.newdoclassC@ivendend.value#=> 1
Otherwise, only the mainRactor can access instance variables of shareable objects.
classC@iv = []# unshareable objectendRactor.newdoclassCbeginp@ivrescueRactor::IsolationErrorp$!.message#=> "can not get unshareable values from instance variables of classes/modules from non-main Ractors"endbegin@iv =42rescueRactor::IsolationErrorp$!.message#=> "can not set instance variables of classes/modules by non-main Ractors"endendend.join
shared =Ractor.new{}shared.instance_variable_set(:@iv,'str')r =Ractor.newshareddo|shared|pshared.instance_variable_get(:@iv)endbeginr.joinrescueRactor::RemoteError=>ee.cause.message#=> can not access instance variables of shareable objects from non-main Ractors (Ractor::IsolationError)end
Note that instance variables for class/module objects are also prohibited on Ractors.
Class variables¶↑
Only the mainRactor can access class variables.
classC@@cv ='str'endr =Ractor.newdoclassCp@@cvendendbeginr.joinrescue=>ee.class#=> Ractor::IsolationErrorend
Constants¶↑
Only the mainRactor can read constants which refer to the unshareable object.
classCCONST ='str'endr =Ractor.newdoC::CONSTendbeginr.joinrescue=>ee.class#=> Ractor::IsolationErrorend
Only the mainRactor can define constants which refer to the unshareable object.
classCendr =Ractor.newdoC::CONST ='str'endbeginr.joinrescue=>ee.class#=> Ractor::IsolationErrorend
To make multi-ractor supported library, the constants should only refer shareable objects.
TABLE = {a:'ko1',b:'ko2',c:'ko3'}
In this case,TABLE references an unshareableHash object. So that other ractors can not referTABLE constant. To make it shareable, we can useRactor.make_shareable() like that.
TABLE =Ractor.make_shareable( {a:'ko1',b:'ko2',c:'ko3'} )
To make it easy, Ruby 3.0 introduced newshareable_constant_value Directive.
# shareable_constant_value: literalTABLE = {a:'ko1',b:'ko2',c:'ko3'}#=> Same as: TABLE = Ractor.make_shareable( {a: 'ko1', b: 'ko2', c: 'ko3'} )
shareable_constant_value directive accepts the following modes (descriptions use the example:CONST = expr):
none: Do nothing. Same as:
CONST = exprliteral:
if
exprconsists of literals, replaced toCONST = Ractor.make_shareable(expr).otherwise: replaced to
CONST = expr.tap{|o| raise unless Ractor.shareable?(o)}.experimental_everything: replaced to
CONST = Ractor.make_shareable(expr).experimental_copy: replaced to
CONST = Ractor.make_shareable(expr, copy: true).
Except thenone mode (default), it is guaranteed that the assigned constants refer to only shareable objects.
Seedoc/syntax/comments.rdoc for more details.
Implementation note¶↑
Each
Ractorhas its own thread, it means eachRactorhas at least 1 native thread.Each
Ractorhas its own ID (rb_ractor_t::pub::id).On debug mode, all unshareable objects are labeled with current Ractor’s id, and it is checked to detect unshareable object leak (access an object from different
Ractor) in VM.
Examples¶↑
Traditional Ring example in Actor-model¶↑
RN =1_000CR =Ractor.currentr =Ractor.newdopRactor.receiveCR<<:finendRN.times{r =Ractor.newrdo|next_r|next_r<<Ractor.receiveend}p:setup_okr<<1pRactor.receive
Fork-join¶↑
deffibnifn<21elsefib(n-2)+fib(n-1)endendRN =10rs = (1..RN).mapdo|i|Ractor.newido|i| [i,fib(i)]endenduntilrs.empty?r,v =Ractor.select(*rs)rs.deleterpanswer:vend
Worker pool¶↑
(1) One ractor has a pool
require'prime'N =1000RN =10# make RN workersworkers = (1..RN).mapdoRactor.newdo|;result_port|loopdon,result_port =Ractor.receiveresult_port<< [n,n.prime?,Ractor.current]endendendresult_port =Ractor::Port.newresults = [](1..N).eachdo|i|ifworkers.empty?# receive a resultn,result,w =result_port.receiveresults<< [n,result]elsew =workers.popend# send a task to the idle worker ractorw<< [i,result_port]end# receive a resultwhileresults.size!=Nn,result,_w =result_port.receiveresults<< [n,result]endppresults.sort_by{|n,result|n}
Pipeline¶↑
# pipeline with send/receiver3 =Ractor.newRactor.currentdo|cr|cr.sendRactor.receive+'r3'endr2 =Ractor.newr3do|r3|r3.sendRactor.receive+'r2'endr1 =Ractor.newr2do|r2|r2.sendRactor.receive+'r1'endr1<<'r0'pRactor.receive#=> "r0r1r2r3"
Supervise¶↑
# ring example againr =Ractor.current(1..10).map{|i|r =Ractor.newr,ido|r,i|r.sendRactor.receive+"r#{i}"end}r.send"r0"pRactor.receive#=> "r0r10r9r8r7r6r5r4r3r2r1"
# ring example with an errorr =Ractor.currentrs = (1..10).map{|i|r =Ractor.newr,ido|r,i|loopdomsg =Ractor.receiveraiseif/e/=~msgr.sendmsg+"r#{i}"endend}r.send"r0"pRactor.receive#=> "r0r10r9r8r7r6r5r4r3r2r1"r.send"r0"pRactor.select(*rs,Ractor.current)#=> [:receive, "r0r10r9r8r7r6r5r4r3r2r1"]r.send"e0"pRactor.select(*rs,Ractor.current)#=># <Thread:0x000056262de28bd8 run> terminated with exception (report_on_exception is true):# Traceback (most recent call last):# 2: from /home/ko1/src/ruby/trunk/test.rb:7:in `block (2 levels) in <main>'# 1: from /home/ko1/src/ruby/trunk/test.rb:7:in `loop'# /home/ko1/src/ruby/trunk/test.rb:9:in `block (3 levels) in <main>': unhandled exception# Traceback (most recent call last):# 2: from /home/ko1/src/ruby/trunk/test.rb:7:in `block (2 levels) in <main>'# 1: from /home/ko1/src/ruby/trunk/test.rb:7:in `loop'# /home/ko1/src/ruby/trunk/test.rb:9:in `block (3 levels) in <main>': unhandled exception# 1: from /home/ko1/src/ruby/trunk/test.rb:21:in `<main>'# <internal:ractor>:69:in `select': thrown by remote Ractor. (Ractor::RemoteError)
# resend non-error messager =Ractor.currentrs = (1..10).map{|i|r =Ractor.newr,ido|r,i|loopdomsg =Ractor.receiveraiseif/e/=~msgr.sendmsg+"r#{i}"endend}r.send"r0"pRactor.receive#=> "r0r10r9r8r7r6r5r4r3r2r1"r.send"r0"pRactor.select(*rs,Ractor.current)[:receive,"r0r10r9r8r7r6r5r4r3r2r1"]msg ='e0'beginr.sendmsgpRactor.select(*rs,Ractor.current)rescueRactor::RemoteErrormsg ='r0'retryend#=> <internal:ractor>:100:in `send': The incoming-port is already closed (Ractor::ClosedError)# because r == r[-1] is terminated.
# ring example with supervisor and re-startdefmake_ractorr,iRactor.newr,ido|r,i|loopdomsg =Ractor.receiveraiseif/e/=~msgr.sendmsg+"r#{i}"endendendr =Ractor.currentrs = (1..10).map{|i|r =make_ractor(r,i)}msg ='e0'# error causing messagebeginr.sendmsgpRactor.select(*rs,Ractor.current)rescueRactor::RemoteErrorr =rs[-1] =make_ractor(rs[-2],rs.size-1)msg ='x0'retryend#=> [:receive, "x0r9r9r8r7r6r5r4r3r2r1"]