Backport #8187
closed
I should have said that this code might exhaust your RAM, so don't let it run too long.
For my case, this code was run in a subprocess. Instead of using Timeout in the subprocess, I use it from the parent instead, like this:
pid = fork { x = " "*(2**31) }
begin
Timeout.timeout(1) do
Process.wait pid
end
rescue Timeout::Error
Process.kill 'SIGKILL', pid
Process.wait pid
end
This solves the problem for me.
Tested against:
- ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.2]
- ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin11.4.2]
The behavior on 1.9.3 is to hang without being able to interrupt, and the behavior
on 2.0.0 appears to execute the code, stop it, and exit without raising an exception.
I couldn't reproduce.
% ruby -v test.rb
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]
/home/kosaki/ruby/git/test.rb:3: warning: assigned but unused variable - x
/home/kosaki/ruby/git/test.rb:3:in *': failed to allocate memory (NoMemoryError) from /home/kosaki/ruby/git/test.rb:3:in
block in '
from /home/kosaki/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/timeout.rb:65:in timeout' from /home/kosaki/ruby/git/test.rb:2:in
'
- Status changed from Open to Rejected
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0