From ae3666b38fd723231c0bbc3c33f4603b6e0e0ef0 Mon Sep 17 00:00:00 2001 From: Jonas Pfenniger Date: Thu, 6 Jan 2011 13:57:59 +0000 Subject: [PATCH] DRb should not forefully close connections on DRb#stop_service When DRb#stop_service is called, all connections get terminated by calling Thread.kill on them. It means that all code that is invoked trough DRb must be exception-safe. This poses the same kind of issues we have with Kernel#timeout. This patch changes the behavior or DRb to only kill the accepting thread, so that no new connections are made. New queries are also forbidden. --- lib/drb/drb.rb | 36 ++++++++++++++---------------------- 1 files changed, 14 insertions(+), 22 deletions(-) diff --git a/lib/drb/drb.rb b/lib/drb/drb.rb index 84a2144..a87d9f1 100644 --- a/lib/drb/drb.rb +++ b/lib/drb/drb.rb @@ -1391,10 +1391,15 @@ module DRb # Stop this server. def stop_service DRb.remove_server(self) - if Thread.current['DRb'] && Thread.current['DRb']['server'] == self - Thread.current['DRb']['stop_service'] = true - else - @thread.kill.join + @thread.kill + + # Check for foreign sub-threads + f = @grp.list.select{|t| t['DRb'].nil?} + $stderr.puts "DRb unknown threads: #{f.inspect}" if f.any? + + # synchronous stop (without current and foreign threads) + while (@grp.list - [Thread.current] - f).size > 0 + Thread.pass end end @@ -1412,21 +1417,9 @@ module DRb end private - def kill_sub_thread - Thread.new do - grp = ThreadGroup.new - grp.add(Thread.current) - list = @grp.list - while list.size > 0 - list.each do |th| - th.kill if th.alive? - end - list = @grp.list - end - end - end def run + @stop_service = false Thread.start do begin while true @@ -1434,7 +1427,7 @@ module DRb end ensure @protocol.close if @protocol - kill_sub_thread + @stop_service = true end end end @@ -1598,11 +1591,10 @@ module DRb end client.send_reply(succ, result) rescue nil ensure - client.close unless succ - if Thread.current['DRb']['stop_service'] - Thread.new { stop_service } + if !succ or @stop_service + client.close + break end - break unless succ end end end -- 1.7.3.4