Trapped signals killing my subprocesses

Hey list,

I have a problem with trapping signals in the main thread whilst a
subprocess is running in another thread. The signal is properly trapped,
however it doesn’t seem to stop the signal from being sent to the
subprocess. Is this an inherent side effect of green thread
implementations?
Can anyone suggest a workaround?

The code here demonstrates my problem. Run it, and hit ^C. My
expectation
was not to see ‘killed :(’ until the 10 seconds are up.

#!/usr/bin/ruby

class Sleeper < Thread

def initialize
super do
IO.popen(‘sleep 10’) {|f| puts f.gets }
puts “killed :(”
end
end
end

Signal.trap(‘INT’) { puts “Signal trapped.” }

a = Sleeper.new
a.join

Cheers
Ian

Yeah that seems like unexpected behavior. I guess ctrl-c is being by
default passed to the subprocess. In my experience ctrl-c is either
passed to any currently ‘being run’ subprocess, OR to Ruby (like begin
… rescue Interrupt…end) but not to both, which happens here. Odd, I
agree.

-Roger

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