JRuby (please use github issues at http://bugs.jruby.org)
  1. JRuby (please use github issues at http://bugs.jruby.org)
  2. JRUBY-6291

Closing One Stream From IO.popen4 Results in Stream Closed Error When Reading Other Streams

    Details

    • Type: Bug Bug
    • Status: Resolved Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: JRuby 1.7.0.pre1
    • Fix Version/s: JRuby 1.7.0.pre1
    • Component/s: None
    • Labels:
      None
    • Environment:
      Java 6
    • Number of attachments :
      0

      Description

      The small snippet below results in an IOError: Stream closed on the stdout_io.each_line line. Removing the stdin.close gets rid of the error.

      IO.popen4("ls") do |pid, stdin, stdout, stderr|
        stdin.close
        [
         Thread.new(stdout) {|stdout_io|
           stdout_io.each_line do |l|
             STDOUT.puts l
             STDOUT.flush
           end
           stdout_io.close
         },
      
         Thread.new(stderr) {|stderr_io|
           stderr_io.each_line do |l|
             STDERR.puts l
             STDERR.flush
           end
         }
        ].each( &:join )
      end
      

      RubyIO's close2 method has code to destroy any spawned processes associated with an IO object that gets closed. So, the workaround is to not close any of the IO streams until the process has finished or the program has finished consuming the process output. See:

      https://github.com/jruby/jruby/blob/master/src/org/jruby/RubyIO.java#L3810
      https://github.com/jruby/jruby/blob/master/src/org/jruby/RubyIO.java#L2002

        Activity

        Hide
        Charles Oliver Nutter added a comment -

        Fixed by master@00d30e1.

        commit 00d30e159ba2cbbe01c04d4fd2b8bd46d9b2ee6a
        Author: Charles Oliver Nutter <headius@headius.com>
        Date:   Tue Dec 20 18:00:38 2011 -0600
        
            Fix JRUBY-6291: Closing One Stream From IO.popen4 Results in Stream Closed Error When Reading Other Streams
            
            Ben figured this one out. The logic I put in place to ensure the
            child process gets nuked for a popen stream was causing it to be
            nuked prematurely for popen3/4 streams. I added a "popenSpecial"
            boolean to RubyIO, so we know not to do that.
        
        Show
        Charles Oliver Nutter added a comment - Fixed by master@00d30e1. commit 00d30e159ba2cbbe01c04d4fd2b8bd46d9b2ee6a Author: Charles Oliver Nutter <headius@headius.com> Date: Tue Dec 20 18:00:38 2011 -0600 Fix JRUBY-6291: Closing One Stream From IO.popen4 Results in Stream Closed Error When Reading Other Streams Ben figured this one out. The logic I put in place to ensure the child process gets nuked for a popen stream was causing it to be nuked prematurely for popen3/4 streams. I added a "popenSpecial" boolean to RubyIO, so we know not to do that.
        Hide
        Ben Browning added a comment -

        Added pull request for test - https://github.com/jruby/jruby/pull/107/

        Show
        Ben Browning added a comment - Added pull request for test - https://github.com/jruby/jruby/pull/107/
        Hide
        Seamus Abshere added a comment -

        This issue also affected open3, so I added another test: https://github.com/jruby/jruby/pull/125

        Show
        Seamus Abshere added a comment - This issue also affected open3, so I added another test: https://github.com/jruby/jruby/pull/125

          People

          • Assignee:
            Charles Oliver Nutter
            Reporter:
            Ben Browning
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: