ahoward / systemu

univeral capture of stdout and stderr and handling of child process pid for windows, *nix, etc.
Other
126 stars 33 forks source link

Using the guard thread correctly #25

Open ploubser opened 11 years ago

ploubser commented 11 years ago

I am struggling to figure out how to correctly use systemu in a multithreaded environment. The use case is as follows.

I have a runner loop that will spawn any number on threads, where each of them could possibly make a call to systemu. The implementation on my side looks like this.

      thread = Thread.current
      @status = systemu(@command, opts) do |cid|
        begin
          while(thread.alive?)
            sleep 0.1
          end

          Process.waitpid(cid) if Process.getpgid(cid)

        rescue SystemExit
        ...

The goal here is to use the thread spawned by systemu to prevent defunct processes.

So the problem is, if the thread that made the call to systemu dies, there obviously is no way for the exit code to make it back to the main thread, leaving me with nil's.

Also, even with the guard thread attempting to clean up defunct processes, I still have users reporting them.

Am I missing something obvious? Any feedback will be greatly appreciated.