diff --git a/lib/open3.rb b/lib/open3.rb index ada451c249..3ee81c30ac 100644 --- a/lib/open3.rb +++ b/lib/open3.rb @@ -207,10 +207,12 @@ module Open3 popen_run(cmd, opts, [in_r, out_w], [in_w, out_r], &block) ensure - in_r.close - in_w.close - out_r.close - out_w.close + if block + in_r.close + in_w.close + out_r.close + out_w.close + end end module_function :popen2e diff --git a/test/test_open3.rb b/test/test_open3.rb index 24bd08e597..47d471c031 100644 --- a/test/test_open3.rb +++ b/test/test_open3.rb @@ -149,6 +149,17 @@ class TestOpen3 < Test::Unit::TestCase } end + def test_popen2e_noblock + i, o, t = Open3.popen2e(RUBY, '-e', 'STDOUT.print STDIN.read') + i.print "baz" + i.close + assert_equal("baz", o.read) + ensure + i.close + o.close + t.join + end + def test_capture3 o, e, s = Open3.capture3(RUBY, '-e', 'i=STDIN.read; print i+"o"; STDOUT.flush; STDERR.print i+"e"', :stdin_data=>"i") assert_equal("io", o)