Callbacks and blocks: why the client needs DRb.start_service
A block or callback object passed to a dRuby server stays in the client and is called back over the network, so the client must run a service too.
Updated
When a client passes a block, or any object that travels by reference, to a remote method, that block or object stays in the client. The server receives a proxy and calls back across the network to use it. For that to work the client must be able to accept incoming calls, which is exactly what DRb.start_service sets up.
A remote method that yields
# server.rb
require 'drb/drb'
class Countdown
def run(from)
from.downto(1) { |n| yield n }
:liftoff
end
end
DRb.start_service('druby://localhost:8787', Countdown.new)
DRb.thread.join
# client.rb
require 'drb/drb'
DRb.start_service
countdown = DRbObject.new_with_uri('druby://localhost:8787')
result = countdown.run(3) { |n| puts n }
p result
The client prints 3, 2, 1 and then :liftoff. The puts runs in the client process, not on the server. Each yield on the server is a network call back to the client.
What happens without start_service
A block is a Proc, and a Proc cannot be marshaled, so dRuby must send a reference to it. A reference is a URI plus an object id, and the URI comes from the local dRuby service. With no service running there is no URI to give, so the call cannot be completed and raises an error in the client (a DRb::DRbConnError on the Ruby version this page was tested with) instead of yielding.
Calling DRb.start_service with no arguments is enough. dRuby picks a free port and has no front object, which is fine: the client only needs to serve the references it hands out.
Callback objects
The same rule applies to objects you pass in. A listener that includes DRb::DRbUndumped is sent by reference and called back later.
# In the client
class Listener
include DRb::DRbUndumped
def update(message)
puts "Got: #{message}"
end
end
listener = Listener.new
feed.subscribe(listener)
Here feed is a remote object whose subscribe method stores its argument and later calls update on it. Keep listener in a variable for as long as the subscription lasts. The server's proxy does not stop the client's garbage collector from reclaiming it, as explained on pass by value and pass by reference.
The network has to work in both directions
A callback is a new connection from the server to the client's URI. That has practical consequences:
- The server must be able to reach the client's host and port. Firewalls and address translation that allow the outgoing connection may block the returning one.
- The client's URI uses the host name the client believes it has. If the server cannot resolve that name, give the client an explicit URI:
DRb.start_service('druby://192.168.1.20:0'). Port0asks for any free port. - On a single machine, start the client with
DRb.start_service('druby://localhost:0')so callbacks stay on the loopback interface.
A client that waits for callbacks
A script that subscribes and then reaches the end of the file will exit. To keep it running and receiving callbacks, finish with DRb.thread.join, as covered on threads and keeping a server alive.