Hanging XMLRPC calls

Hi!

I have a problem with hanging XMLRPC calls. It appears when I call
XMLRPC::Client.new too freqently. For my own sake, I have a work around
for this (I save the server reference in an instance variable, for
example).

But this might be of interest for other people. And my idea was to show
this for you Ruby people with more insight into this area.

As you can see below sometimes the call hangs for 30 seconds. Note that
it appears even when sleeping 5 seconds between the instantiations.

This is a really simple piece of code!

Best Regards
///Rickard

Here is the code:

#!/app/ruby/1.8.x/bin/ruby

require “xmlrpc/client”

Server code (from

http://tldp.org/HOWTO/XML-RPC-HOWTO/xmlrpc-howto-ruby.html):
##!/app/ruby/1.8.x/bin/ruby
#require “xmlrpc/server”
#s = XMLRPC::Server.new(4001)

#class MyHandler

def sumAndDifference(a, b)

{ “sum” => a + b, “difference” => a - b }

end

#end

#s.add_handler(“sample”, MyHandler.new)
#s.serve
##end of server code

def call_server(sleeptime)
puts “**** Trying with sleeptime #{sleeptime} sec ****”

Create server reference outside of loop (this works fine)

#server = XMLRPC::Client.new(“127.0.0.1”, “/”, 4001)

for i in 1…7

# Create server reference inside of loop (this creates the problem,

when called frequently)
server = XMLRPC::Client.new(“127.0.0.1”, “/”, 4001)

start = Time.now.tv_sec
result = server.call("sample.sumAndDifference", 5, 3)
total = Time.now.tv_sec - start

# print result and number of seconds spent during the remote call
puts "Result: #{result.inspect}. Time: #{total} sec"

# just to show we dont need to stress "that much"
# it seems that it doensnt hang when sleep >= 10 sec (on my linux

machine)
Kernel.sleep(sleeptime)

end
end

[0,5,7,10].each do |sleeptime|
call_server(sleeptime)
end

Here is the output:

; ./client.rb
**** Trying with sleeptime 0 sec ****
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 30 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
**** Trying with sleeptime 5 sec ****
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 20 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 10 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
**** Trying with sleeptime 7 sec ****
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 4 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 2 sec
**** Trying with sleeptime 10 sec ****
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec
Result: {“sum”=>8, “difference”=>2}. Time: 0 sec

I have been told that this problem occurs since the garbage collector
doesnt close the new sockets in the same rate as I create new ones.

Then, the first idea was to use a “close” method, but of what I can see
it doesnt exist. So instead I tried to force the garbage collector to
run, which actually works. But, is it a good idea to do this? Some
other, better way to acheive this?

loop

client = Client.new
client.call(…)

#client.close #doesnt work, “undefined method”

ObjectSpace.garbage_collect #works fine

end

Regards
/Rickard

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs