Trapping INT in Thread

Hi

I’ve spend a fair amount of time today on a thing that puzzles me.

Trapping signals in a thread has varying outcome depending on the method
used.

The following code will ignore CTRL-C in a terminal and won’t die until
the sleep process exists after 3 seconds.

puts ‘sleeping’

t=Thread.new do
trap(“INT”, “IGNORE”)
sleep 3
end

t.join

puts “exiting”

However, trapping the signal like so, will also signal sleep (and kill
it).

puts ‘sleeping’

t=Thread.new do
trap(“INT”) { puts ‘got int’ }
sleep 3
end

t.join

puts “exiting”

I’m aware that all the children of the process should receive the
signal. I just don’t understand the difference in outcome (I know that
the implementation in MRI is different).

Ideally what I’d like to have is a solution where I’m able to CTRL-C a
multithreaded program and not have it kill its children.

Hope this makes sense :slight_smile:

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