summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMatthias H <apoc@sixserv.org>2013-09-18 03:18:13 +0200
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2013-11-02 11:30:39 +0100
commit6355305f4375b72b457d66304ac001c7bd89144c (patch)
tree3409ec1c15a3bf7953d194e3285bbcb733a362e5 /lib
parentfa08e1bd0b4c915add758931a5b47d2c74ebddc0 (diff)
ruby 2.0.0: better fix for the ThreadError
I revisited my fix for the ThreadError in ruby 2.0.0 this doesn't use a queue for signals but just starts a thread for each received signal, works much better for me. It also fixes a bug in Bot#reconnect where the SystemExit exception coming from quit is not handled correctly.
Diffstat (limited to 'lib')
-rw-r--r--lib/rbot/ircbot.rb56
1 files changed, 29 insertions, 27 deletions
diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb
index 5713d175..d75d6b54 100644
--- a/lib/rbot/ircbot.rb
+++ b/lib/rbot/ircbot.rb
@@ -801,8 +801,7 @@ class Bot
:purge_split => @config['send.purge_split'],
:truncate_text => @config['send.truncate_text'].dup
- @signals = []
- trap_sigs
+ trap_signals
end
def repopulate_botclass_directory
@@ -905,34 +904,32 @@ class Bot
end
# things to do when we receive a signal
- def handle_sigs
- while sig = @signals.shift
- func = case sig
- when 'SIGHUP'
- :restart
- when 'SIGUSR1'
- :reconnect
- else
- :quit
- end
- debug "received #{sig}, queueing #{func}"
- # this is not an interruption if we just need to reconnect
- $interrupted += 1 unless func == :reconnect
- self.send(func) unless @quit_mutex.locked?
- debug "interrupted #{$interrupted} times"
- if $interrupted >= 3
- debug "drastic!"
- log_session_end
- exit 2
- end
+ def handle_signal(sig)
+ func = case sig
+ when 'SIGHUP'
+ :restart
+ when 'SIGUSR1'
+ :reconnect
+ else
+ :quit
+ end
+ debug "received #{sig}, queueing #{func}"
+ # this is not an interruption if we just need to reconnect
+ $interrupted += 1 unless func == :reconnect
+ self.send(func) unless @quit_mutex.locked?
+ debug "interrupted #{$interrupted} times"
+ if $interrupted >= 3
+ debug "drastic!"
+ log_session_end
+ exit 2
end
end
# trap signals
- def trap_sigs
+ def trap_signals
begin
%w(SIGINT SIGTERM SIGHUP SIGUSR1).each do |sig|
- trap(sig) { @signals << sig }
+ trap(sig) { Thread.new { handle_signal sig } }
end
rescue ArgumentError => e
debug "failed to trap signals (#{e.pretty_inspect}): running on Windows?"
@@ -997,6 +994,9 @@ class Bot
end
connect
+ rescue SystemExit
+ log_session_end
+ exit 0
rescue DBFatal => e
fatal "fatal db error: #{e.pretty_inspect}"
DBTree.stats
@@ -1016,12 +1016,10 @@ class Bot
quit_msg = nil
valid_recv = false # did we receive anything (valid) from the server yet?
begin
- handle_sigs
reconnect(quit_msg, too_fast)
quit if $interrupted > 0
valid_recv = false
while @socket.connected?
- handle_sigs
quit if $interrupted > 0
# Wait for messages and process them as they arrive. If nothing is
@@ -1322,7 +1320,11 @@ class Bot
save
debug "\tcleaning up ..."
@save_mutex.synchronize do
- @plugins.cleanup
+ begin
+ @plugins.cleanup
+ rescue
+ debug "\tignoring cleanup error: #{$!}"
+ end
end
# debug "\tstopping timers ..."
# @timer.stop