Thread, external processes, and trap

Discussion in 'Ruby' started by James Mills, Sep 11, 2006.

  1. James Mills

    James Mills Guest

    Hello list.

    I have what I hope is a simple newbie problem.

    I have a ruby program that is acting as a "controller" between a
    remote application and an external process. Basically the controller
    receives a command from the remote application, does some quick
    formatting, then connects to the remote process via an admin interface
    and sends the command.

    In order to ensure that I have an external process to send information
    to, I spawn it and watch it from within the ruby controller. I also
    want to keep an eye on it and re-spawn it if needed. So basically I
    used Thread.new twice. The first thread is the process "watcher"
    which spans the actual process thread. Once it does this, the watcher
    thread sleeps until the process thread exits. If the process does
    exit for some reason, the watcher thread will respawn it a
    configurable number of times.

    This actually works pretty good, in and of itself. The problem comes
    when I introduce a SIGINT trap in the main body of my code. The INT
    trap simply cleans up the running threads and exits. This also works
    fine by itself.

    The problem is when I try and use ^C to exit the program *after* I've
    already respawned the process thread. Here are a few scenarios:

    1) watcher and proc threads up and running with no respawns. ^C is
    pressed - App exits as expected

    2) watcher and proc threads up and running. I kill the external
    process manually - The watcher thread catches the fact that the
    process thread is no longer running and respawns it

    3) watcher and proc threads up and running. I kill the external
    process manually, and the watcher respawns it. I press ^C - The
    process thread is killed and the watcher catches and respawns it! The
    trap code is never executed!!!!


    This problem has really frustrated me, and I hope the list can give me
    a hand. This is my first app using Threads, so go easy...

    Thanks,
    ~james
     
    James Mills, Sep 11, 2006
    #1
    1. Advertising

  2. James Mills wrote:
    > In order to ensure that I have an external process to send information
    > to, I spawn it and watch it from within the ruby controller. I also
    > want to keep an eye on it and re-spawn it if needed. So basically I
    > used Thread.new twice. The first thread is the process "watcher"
    > which spans the actual process thread. Once it does this, the watcher
    > thread sleeps until the process thread exits. If the process does
    > exit for some reason, the watcher thread will respawn it a
    > configurable number of times.


    > 3) watcher and proc threads up and running. I kill the external
    > process manually, and the watcher respawns it. I press ^C - The
    > process thread is killed and the watcher catches and respawns it! The
    > trap code is never executed!!!!


    That's probably because a) the trap block is executed in the context of
    the watcher thread and b) exit uses an exception to terminate the process:

    robert@fussel /cygdrive/c/Temp
    $ ruby -e 'begin exit; rescue Exception => e; p e end'
    #<SystemExit: exit>

    robert@fussel /cygdrive/c/Temp
    $ ruby -e 'puts SystemExit.ancestors'
    SystemExit
    Exception
    Object
    Kernel

    I guess you catch Exception in your watcher and not something more
    specific. You should probably either catch more selective or check the
    exception and act accordingly in your watcher thread.

    Kind regards

    robert
     
    Robert Klemme, Sep 11, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darren Smith

    External Processes

    Darren Smith, Oct 12, 2004, in forum: Perl
    Replies:
    1
    Views:
    485
    J├╝rgen Exner
    Oct 12, 2004
  2. John English
    Replies:
    6
    Views:
    481
    John English
    Oct 27, 2005
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,210
    David Schwartz
    Dec 9, 2003
  4. ChokSheak Lau

    C external execution - trap stdout + stderr

    ChokSheak Lau, Sep 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    294
    CBFalconer
    Sep 16, 2004
  5. Marc Heiler
    Replies:
    1
    Views:
    192
    Robert Klemme
    May 24, 2009
Loading...

Share This Page