Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeout::Error exception in a loop

I have this piece of ruby code in a loop:

pid = Process.spawn("my_process_command")
begin
    Timeout.timeout(16) do
        `my_timeout_command`
        Process.wait(pid)
    end
rescue
    system("clear")
    puts 'Process not finished in time, killing it'
    Process.kill('TERM', pid)
end

The problem is that once the Timeout::Error exception has been caught the block will be skipped and the loop does practically nothing. How can I fix this?

like image 669
DeliciousPie Avatar asked Jul 12 '26 08:07

DeliciousPie


1 Answers

You need to rescue specifically for Timeout:Error since Timeout::Error is not a standard error:

pid = Process.spawn("my_process_command")
begin
    Timeout.timeout(16) do
        `my_timeout_command`
        Process.wait(pid)
    end
rescue Timeout::Error
    system("clear")
    puts 'Process not finished in time, killing it'
    Process.kill('TERM', pid)
end
like image 197
Uri Agassi Avatar answered Jul 15 '26 01:07

Uri Agassi