Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Terminator: A Hardcore “Timeout” For Ruby Code

By Peter Cooper / September 14, 2008

terminator.png

Terminator is a new library developed by Ara Howard and Mikel Lindsaar that solves the problem of Ruby's own "timeout" feature not actually being up to the task of terminating code. timeout is supposed to terminate the execution of a block of code if it's not finished within a certain amount of time, but due to Ruby's threading model it doesn't always work that way!

Initially, Mikel considered SystemTimer - another replacement for timeout. SystemTimer is based upon UNIX alarm signals and, for some reason, still didn't resolve the main problem (although SystemTimer has ridiculously good documentation and is clearly working well for someone). After lamenting on the Ruby-Talk mailing list, Ara Howard stepped in with a potential solution which turned into what is now Terminator.

Terminator works by starting up another Ruby instance that tracks the PID of the Ruby instance that spawned it. Windows is currently not supported.

You can install Terminator with gem install terminator or check out the code directly from here (note: they have some weird issue with referrals, just reload the URL when you get there).

Comments

  1. Mikel Lindsaar says:

    Thanks for the write up.

    I just want people to know this is mainly Ara's work. I helped with the name, some specs, testing as well as a couple of patches for the 0.4.3 release and some documentation. But Ara should have the credit for the library.

    Mikel

  2. Alan Brown says:

    So far SystemTimer is sounding better. Not sure what the problems with SystemTimer are and there are no links offered regarding them.

    Also, the link for checking out the code got me this message:

    Precondition Failed

    The precondition on the request for the URL /lib/ruby/terminator/terminator-0.4.2/ evaluated to false.

  3. Peter Cooper says:

    They have some odd issue if you come from a site that isn't theirs, by the looks of it. If you reload once you're there, it appears as it should. They must have some HTTP precondition regarding loading their pages..

  4. Hongli Lai says:

    I wouldn't be so quick to blame Ruby's threading model for this. System call interruption is not easy to do, whether you use native (kernel) threads or userspace threads. If you want the thing to work on Windows, then be prepared for a huge amount of work. For Phusion Passenger I had to write an entire system call interruption framework just to emulate Java-style thread interruption that also works with blocking system calls (this is used during the spawn server's shutdown procedure).

    As for Terminator, be aware that any data you produce within the block can only be sent to outside the block via a communication channel, e.g. a pipe or a file. Terminator is also not compatible with JRuby because JRuby (and Java) doesn't support fork().

Other Posts to Enjoy

Twitter Mentions