Threads raising exceptions

I have the following piece of threading code:

Thread.new(i) do |j|
#$SAFE = 4
b = 0
loop do
b += 1
raise ‘Test’ if j == 3 and b == 100
sleep(0)
end
end

This behaves as expected - if Thread.abort_on_exception is true, the program
throws an exception and dies, otherwise just this thread dies, and no
exception I can rescue. So far, so good.

If, however, I uncomment the line with $SAFE = 4, it behaves as if
Thread.abort_on_exception is false, and so I can’t catch any exception when a
thread dies, and so I can’t find out what happened, which I need to! What can
I do about this?

Tim Bates

···


tim@bates.id.au

Hi,

···

In message “Threads raising exceptions” on 02/12/18, Tim Bates tim@bates.id.au writes:

I have the following piece of threading code:

Thread.new(i) do |j|
#$SAFE = 4
b = 0
loop do
b += 1
raise ‘Test’ if j == 3 and b == 100
sleep(0)
end
end

This behaves as expected - if Thread.abort_on_exception is true, the program
throws an exception and dies, otherwise just this thread dies, and no
exception I can rescue. So far, so good.

If, however, I uncomment the line with $SAFE = 4, it behaves as if
Thread.abort_on_exception is false, and so I can’t catch any exception when a
thread dies, and so I can’t find out what happened, which I need to! What can
I do about this?

use Thread#join or Thread#value to retrieve exception from the dead
thread. It is so to avoid process termination by exceptions from
untrusted code. For debugging, $DEBUG=true will print exception names.

						matz.