Forum: Ruby How to timeout a system call

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ff6312b9dfc795eea0b6ab90884011aa?d=identicon&s=25 Jordi Planes (jordi)
on 2007-07-02 14:13
Hi,

I would like to stop a system call after a timeout, but doing so with
the Timeout library does not kill the process, and the process keeps
running. I think the problem is the system call does a Process.fork and
no way to kill it, no matter if the Thread is stoped.

Is there any way to kill the process? May be getting its pid?

Thanks,

Jordi
Ff6312b9dfc795eea0b6ab90884011aa?d=identicon&s=25 Jordi Planes (jordi)
on 2007-07-03 13:26
Jordi Planes wrote:
>
> Hi,
>
> I would like to stop a system call after a timeout, but doing so with
> the Timeout library does not kill the process, and the process keeps
> running. I think the problem is the system call does a Process.fork and
> no way to kill it, no matter if the Thread is stoped.
>
> Is there any way to kill the process? May be getting its pid?
>
> Thanks,
>
> Jordi



I finally come up with my own timeout, using Process instead of Thread.
In case somebody is interested, here it is:

module Timeout

  class Error<Interrupt
  end

  def timeout( sec, exception=Error )
    return yield if sec == nil or sec.zero?
    pid_execution = Process.fork { begin yield ensure exit! end }
    pid_sleep = Process.fork { begin sleep sec ensure exit! end }
    if Process.wait == pid_sleep then
      Process.kill( 'INT', pid_execution )
      raise exception, "execution expired"
    else
      Process.kill( 'ALRM', pid_sleep )
    end
  end

  module_function :system_call, :timeout

end
Ff6312b9dfc795eea0b6ab90884011aa?d=identicon&s=25 Jordi Planes (jordi)
on 2007-07-13 17:40
This is even nicer:
http://www.jpaisley.com/software/ruby/open4.rb

Good job Jonathan!



Jordi Planes wrote:
> Jordi Planes wrote:
>>
>> Hi,
>>
>> I would like to stop a system call after a timeout, but doing so with
>> the Timeout library does not kill the process, and the process keeps
>> running. I think the problem is the system call does a Process.fork and
>> no way to kill it, no matter if the Thread is stoped.
>>
>> Is there any way to kill the process? May be getting its pid?
>>
>> Thanks,
>>
>> Jordi
>
>
>
> I finally come up with my own timeout, using Process instead of Thread.
> In case somebody is interested, here it is:
>
> module Timeout
>
>   class Error<Interrupt
>   end
>
>   def timeout( sec, exception=Error )
>     return yield if sec == nil or sec.zero?
>     pid_execution = Process.fork { begin yield ensure exit! end }
>     pid_sleep = Process.fork { begin sleep sec ensure exit! end }
>     if Process.wait == pid_sleep then
>       Process.kill( 'INT', pid_execution )
>       raise exception, "execution expired"
>     else
>       Process.kill( 'ALRM', pid_sleep )
>     end
>   end
>
>   module_function :system_call, :timeout
>
> end
This topic is locked and can not be replied to.