Using open3 in ruby to call an interactive shell command with all three pipes while avoiding block

Apparently when using the three standard pipes (stdin, stdout, stderr)
in your own programming, it is REALLY easy to block or deadlock on
them. At first I figured there were problems in the ruby *open3
library, but this is not ruby specific, I found message board
discussions about similar problems using perl open3 libraries.

Trying to figure out these quirks is made harder by the fact that all
of the common ruby examples are non-interactive, and just close the
stdin before trying to read any output. Even worse, many, many shell
programs don't use stderr and stdout in what I understood as the
proper way. For instance I first as trying to use ftp as the command,
but this proved beyond my abilities. I then came up with a simple
working example that calls dc (the simple reverse polish calculator
available on most unixes), and discovered that on errors it outputs to
both stderr and stdout (different text). I was not able to in any way
to get the output from stderr until I closed stdin (short of
redirecting stderr to a temp file).

This drives home the point that you shouldn't be doing this too much,
in ruby. Generally, use a better, task-specific ruby library, for
these sort of tasks. If you want to use ftp, use net-ftp, for
instance. If you want to use more extensive interactive shell
sessions, check out Ara Howard's session
gem

*Open3 as a naming convention tends to be a library to call shell
commands with the three aforementioned pipes.

Filed under  //   jruby   ruby  

Comments (2)

Oct 24, 2011
Thanks for writing this up. I describe an alternate approach using Ruby's non-blocking IO#ready? method here:

http://illuminatedcomputing.com/blog/?p=288

Oct 24, 2011
Tim Connor said...
Ah, thanks, that's a nice clean way to handle that.

Leave a comment...