Thread, external processes, and trap

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 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.

  1. 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