Why does this hang?

Howdy,

I’ve got a simple test routine using Open3.popen3 and it hangs when checking
for EOF on the stderr IO object that Open3.popen3 returns. Here’s the
simple program:

def run_command
stdin, out, err = Open3.popen3(“cat /etc/ifhp.conf”)

  while not out.eof? and not err.eof?
    select_on = []
    select_on.push(out) unless out.eof?
    select_on.push(err) unless err.eof?

    r = select(select_on)
    p r

    r[0].each { |fh|
if fh == out
  puts "out: #{fh.gets.to_s}"
end
if fh == err
  puts "err: #{fh.gets.to_s}"
end
    }
    $stdout.flush
end

end

run_command

When I run this, it hangs at the “while not …” line. If I don’t check for
"err.eof?", then it never hangs.

I don’t understand what is going on. Any help/guidance/suggestions will
be greatly appreciated.

Jeff.

···


Jeff Putsch Email: putsch@mxim.com
Maxim Integrated Products Office: (503)547-2037
High Frequency CAD Engineering

Hi,

I’ve got a simple test routine using Open3.popen3 and it hangs when checking
for EOF on the stderr IO object that Open3.popen3 returns. Here’s the
simple program:

IO#eof? tries to read next data to examin EOF reached. Use
select before eof?.

def run_command
stdin, out, err = Open3.popen3(“cat /etc/ifhp.conf”)

select_on = [out, err]
while !select_on.empty? and r = select(select_on)
  p r

  r[0].each {|fh|
    if fh.eof?
      select_on.delete(fh)
      next
    elsif fh == out
      puts "out: #{fh.gets.to_s}"
    elsif fh == err
      puts "err: #{fh.gets.to_s}"
    end
  }
  $stdout.flush
end

end

···

At Fri, 23 Aug 2002 07:35:38 +0900, Jeff Putsch wrote:


Nobu Nakada

Much thanks to Nobu for his explanation and his correction of my sample code.
I’m no longer stuck and understand not only how to do this, but why!

Jeff.

···

On Fri, Aug 23, 2002 at 11:32:46AM +0900, nobu.nokada@softhome.net wrote:

IO#eof? tries to read next data to examin EOF reached. Use
select before eof?.

def run_command
stdin, out, err = Open3.popen3(“cat /etc/ifhp.conf”)

select_on = [out, err]
while !select_on.empty? and r = select(select_on)
  p r

  r[0].each {|fh|
    if fh.eof?
      select_on.delete(fh)
      next
    elsif fh == out
      puts "out: #{fh.gets.to_s}"
    elsif fh == err
      puts "err: #{fh.gets.to_s}"
    end
  }
  $stdout.flush
end

end

Jeff Putsch Email: putsch@mxim.com
Maxim Integrated Products Office: (503)547-2037
High Frequency CAD Engineering