Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Steals INT if ran in a thread that dies prematurely #5

Open
ripienaar opened this issue Feb 24, 2011 · 1 comment
Open

Steals INT if ran in a thread that dies prematurely #5

ripienaar opened this issue Feb 24, 2011 · 1 comment

Comments

@ripienaar
Copy link
Contributor

I have a complex case where I want to use systemu to run programs but the thread that happens in might sometimes die.

here's a simple bit of code that demonstrates:

Signal.trap("INT") do
    puts "Int!"
end

Process.kill(2, $$)

s = Thread.new {
    systemu("sleep 10") do |pid|
        Process.waitpid(pid)
    end
}

sleep 4

s.terminate
puts "terminated thread..."

sleep 10

3.times {
    Process.kill(2, $$)
    puts "self killed..."
    sleep 0.5
}

Process.kill(9, $$)

If you run this you'll see the first Int gets received, it then starts a thread that sleeps for 10 seconds - but 4 seconds into the 10 the thread gets killed.

The sleep continues till its past its 10 seconds - thats great and what I want - but after that the INT signal is ignored.

The calling thread wont exit often but its possible something can go wrong and so loosing INT is a problem. In cases where the thread doesn't exit then INT is still fine

@ahoward
Copy link
Owner

ahoward commented Mar 3, 2014

you probably want to use this idiom to organize your pids before you starting handling signals

q = Queue.new

Thread.new{ systemu('sleep 10'){|pid| q.push(pid)} }

pid = q.pop

causing an entire process to wait from inside a thread is meaningless in ruby: everything stops there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants